您可以使用Duktape 属性 API 调用来执行此操作,例如如下:
duk_ret_t modify_object(duk_context *ctx) {
/* argument object is at index 0 */
duk_push_uint(ctx, 123); /* -> stack: [ obj 123 ] */
duk_put_prop_string(ctx, 0, "new_param"); /* -> stack: [ obj ] */
duk_push_uint(ctx, 456);
duk_put_prop_string(ctx, 0, "existing_param2");
del_prop_string(ctx, 0, "existing_param3");
return 0; /* no return value (same as 'return undefined' in Ecmascript) */
}
该函数将被注册为具有单个参数:对象,它将出现在值堆栈索引 0 处:
duk_push_c_function(ctx, modify_object, 1 /*nargs*/);
duk_put_global_string(ctx, "modifyObject"); /* register global binding */