/* * call-seq: * call_prop(name, params,...) -> obj * call_prop([names,...], params,...) -> obj * * Call a function defined in the global scope with the given parameters. An * Array of names can be given to call a function on a nested object. * * ctx.call_prop("parseInt", "42") #=> 42 * ctx.call_prop(["Math", "pow"], 2, 10) #=> 1024 * */ static VALUE ctx_call_prop(int argc, VALUE* argv, VALUE self) { struct state *state; Data_Get_Struct(self, struct state, state); VALUE prop; VALUE *prop_args; rb_scan_args(argc, argv, "1*", &prop, &prop_args); ctx_get_nested_prop(state, prop); // Swap receiver and function duk_swap_top(state->ctx, -2); // Push arguments for (int i = 1; i < argc; i++) { ctx_push_ruby_object(state, argv[i]); } if (duk_pcall_method(state->ctx, (argc - 1)) == DUK_EXEC_ERROR) { raise_ctx_error(state); } VALUE res = ctx_stack_to_value(state, -1); duk_set_top(state->ctx, 0); return res; }
duk_int_t duk_peval_file(duk_context *ctx, const char *path) { duk_int_t rc; duk_push_string_file_raw(ctx, path, DUK_STRING_PUSH_SAFE); duk_push_string(ctx, path); rc = duk_pcompile(ctx, DUK_COMPILE_EVAL); if (rc != 0) { return rc; } duk_push_global_object(ctx); /* 'this' binding */ rc = duk_pcall_method(ctx, 0); return rc; }
AJ_Status AJS_ServiceIO(duk_context* ctx) { AJS_IO_PinTriggerCondition condition; int32_t trigId; trigId = AJS_TargetIO_PinTrigId(&condition); if (trigId != AJS_IO_PIN_NO_TRIGGER) { AJ_InfoPrintf(("triggered on id %d\n", trigId)); /* * Lookup the pin object in the triggers array */ duk_get_global_string(ctx, AJS_IOObjectName); duk_get_prop_string(ctx, -1, AJS_HIDDEN_PROP("trigs")); do { duk_get_prop_index(ctx, -1, trigId); if (duk_is_object(ctx, -1)) { /* * Call the trigger function passing the pin object and value as the argument */ duk_get_prop_string(ctx, -1, "trigger"); if (duk_is_callable(ctx, -1)) { /* * Pin object is the "this" object */ duk_dup(ctx, -2); duk_push_int(ctx, condition); if (duk_pcall_method(ctx, 1) != DUK_EXEC_SUCCESS) { AJS_ConsoleSignalError(ctx); } } /* * Pop pin object */ duk_pop(ctx); } else { AJ_ErrPrintf(("Expected a pin object trigId = %d\n", trigId)); } duk_pop(ctx); trigId = AJS_TargetIO_PinTrigId(&condition); } while (trigId != AJS_IO_PIN_NO_TRIGGER); duk_pop_2(ctx); } return AJ_OK; }
/* Eval is just a wrapper now. */ DUK_EXTERNAL duk_int_t duk_eval_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) { duk_uint_t comp_flags; duk_int_t rc; DUK_ASSERT_CTX_VALID(ctx); /* Note: strictness is *not* inherited from the current Duktape/C. * This would be confusing because the current strictness state * depends on whether we're running inside a Duktape/C activation * (= strict mode) or outside of any activation (= non-strict mode). * See tests/api/test-eval-strictness.c for more discussion. */ /* [ ... source? filename? ] (depends on flags) */ comp_flags = flags; comp_flags |= DUK_COMPILE_EVAL; rc = duk_compile_raw(ctx, src_buffer, src_length, comp_flags); /* may be safe, or non-safe depending on flags */ /* [ ... closure/error ] */ if (rc != DUK_EXEC_SUCCESS) { rc = DUK_EXEC_ERROR; goto got_rc; } duk_push_global_object(ctx); /* explicit 'this' binding, see GH-164 */ if (flags & DUK_COMPILE_SAFE) { rc = duk_pcall_method(ctx, 0); } else { duk_call_method(ctx, 0); rc = DUK_EXEC_SUCCESS; } /* [ ... result/error ] */ got_rc: if (flags & DUK_COMPILE_NORESULT) { duk_pop(ctx); } return rc; }
static duk_ret_t test_func(duk_context *ctx, void *udata) { (void) udata; if (ctx) { printf("dummy - return here\n"); fflush(stdout); return 0; } /* Up-to-date for Duktape 1.3.0, alphabetical order: * $ cd website/api; ls *.yaml */ (void) duk_alloc_raw(ctx, 0); (void) duk_alloc(ctx, 0); (void) duk_base64_decode(ctx, 0); (void) duk_base64_encode(ctx, 0); (void) duk_buffer_to_string(ctx, 0); (void) duk_call_method(ctx, 0); (void) duk_call_prop(ctx, 0, 0); (void) duk_call(ctx, 0); (void) duk_char_code_at(ctx, 0, 0); (void) duk_check_stack_top(ctx, 0); (void) duk_check_stack(ctx, 0); (void) duk_check_type_mask(ctx, 0, 0); (void) duk_check_type(ctx, 0, 0); (void) duk_compact(ctx, 0); (void) duk_compile_lstring_filename(ctx, 0, "dummy", 0); (void) duk_compile_lstring(ctx, 0, "dummy", 0); (void) duk_compile_string_filename(ctx, 0, "dummy"); (void) duk_compile_string(ctx, 0, "dummy"); (void) duk_compile(ctx, 0); (void) duk_concat(ctx, 0); (void) duk_config_buffer(ctx, 0, NULL, 0); (void) duk_copy(ctx, 0, 0); (void) duk_create_heap_default(); (void) duk_create_heap(NULL, NULL, NULL, NULL, NULL); (void) duk_debugger_attach(ctx, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); (void) duk_debugger_cooperate(ctx); (void) duk_debugger_detach(ctx); (void) duk_debugger_notify(ctx, 0); (void) duk_debugger_pause(ctx); (void) duk_decode_string(ctx, 0, NULL, NULL); (void) duk_def_prop(ctx, 0, 0); (void) duk_del_prop_index(ctx, 0, 0); (void) duk_del_prop_string(ctx, 0, "dummy"); (void) duk_del_prop(ctx, 0); (void) duk_destroy_heap(ctx); (void) duk_dump_function(ctx); (void) duk_dup_top(ctx); (void) duk_dup(ctx, 0); (void) duk_enum(ctx, 0, 0); (void) duk_equals(ctx, 0, 0); duk_error_va(ctx, 0, NULL, NULL); duk_error(ctx, 0, "dummy"); /* (void) cast won't work without variadic macros */ (void) duk_eval_lstring_noresult(ctx, "dummy", 0); (void) duk_eval_lstring(ctx, "dummy", 0); (void) duk_eval_noresult(ctx); (void) duk_eval_string_noresult(ctx, "dummy"); (void) duk_eval_string(ctx, "dummy"); (void) duk_eval(ctx); (void) duk_fatal(ctx, "dummy"); (void) duk_free_raw(ctx, NULL); (void) duk_free(ctx, NULL); (void) duk_gc(ctx, 0); (void) duk_get_boolean(ctx, 0); (void) duk_get_buffer_data(ctx, 0, NULL); (void) duk_get_buffer(ctx, 0, NULL); (void) duk_get_c_function(ctx, 0); (void) duk_get_context(ctx, 0); (void) duk_get_current_magic(ctx); (void) duk_get_error_code(ctx, 0); (void) duk_get_finalizer(ctx, 0); (void) duk_get_global_string(ctx, 0); (void) duk_get_heapptr(ctx, 0); (void) duk_get_int(ctx, 0); (void) duk_get_length(ctx, 0); (void) duk_get_lstring(ctx, 0, NULL); (void) duk_get_magic(ctx, 0); (void) duk_get_memory_functions(ctx, NULL); (void) duk_get_number(ctx, 0); (void) duk_get_pointer(ctx, 0); (void) duk_get_prop_index(ctx, 0, 0); (void) duk_get_prop_string(ctx, 0, "dummy"); (void) duk_get_prop(ctx, 0); (void) duk_get_prototype(ctx, 0); (void) duk_get_string(ctx, 0); (void) duk_get_top_index(ctx); (void) duk_get_top(ctx); (void) duk_get_type_mask(ctx, 0); (void) duk_get_type(ctx, 0); (void) duk_get_uint(ctx, 0); (void) duk_has_prop_index(ctx, 0, 0); (void) duk_has_prop_string(ctx, 0, "dummy"); (void) duk_has_prop(ctx, 0); (void) duk_hex_decode(ctx, 0); (void) duk_hex_encode(ctx, 0); (void) duk_insert(ctx, 0); (void) duk_instanceof(ctx, 0, 0); (void) duk_is_array(ctx, 0); (void) duk_is_boolean(ctx, 0); (void) duk_is_bound_function(ctx, 0); (void) duk_is_buffer(ctx, 0); (void) duk_is_callable(ctx, 0); (void) duk_is_c_function(ctx, 0); (void) duk_is_constructor_call(ctx); (void) duk_is_dynamic_buffer(ctx, 0); (void) duk_is_ecmascript_function(ctx, 0); (void) duk_is_error(ctx, 0); (void) duk_is_eval_error(ctx, 0); (void) duk_is_fixed_buffer(ctx, 0); (void) duk_is_function(ctx, 0); (void) duk_is_lightfunc(ctx, 0); (void) duk_is_nan(ctx, 0); (void) duk_is_null_or_undefined(ctx, 0); (void) duk_is_null(ctx, 0); (void) duk_is_number(ctx, 0); (void) duk_is_object_coercible(ctx, 0); (void) duk_is_object(ctx, 0); (void) duk_is_pointer(ctx, 0); (void) duk_is_primitive(ctx, 0); (void) duk_is_range_error(ctx, 0); (void) duk_is_reference_error(ctx, 0); (void) duk_is_strict_call(ctx); (void) duk_is_string(ctx, 0); (void) duk_is_syntax_error(ctx, 0); (void) duk_is_thread(ctx, 0); (void) duk_is_type_error(ctx, 0); (void) duk_is_undefined(ctx, 0); (void) duk_is_uri_error(ctx, 0); (void) duk_is_valid_index(ctx, 0); (void) duk_join(ctx, 0); (void) duk_json_decode(ctx, 0); (void) duk_json_encode(ctx, 0); (void) duk_load_function(ctx); (void) duk_map_string(ctx, 0, NULL, NULL); (void) duk_new(ctx, 0); (void) duk_next(ctx, 0, 0); (void) duk_normalize_index(ctx, 0); (void) duk_pcall_method(ctx, 0); (void) duk_pcall_prop(ctx, 0, 0); (void) duk_pcall(ctx, 0); (void) duk_pcompile_lstring_filename(ctx, 0, "dummy", 0); (void) duk_pcompile_lstring(ctx, 0, "dummy", 0); (void) duk_pcompile_string_filename(ctx, 0, "dummy"); (void) duk_pcompile_string(ctx, 0, "dummy"); (void) duk_pcompile(ctx, 0); (void) duk_peval_lstring_noresult(ctx, "dummy", 0); (void) duk_peval_lstring(ctx, "dummy", 0); (void) duk_peval_noresult(ctx); (void) duk_peval_string_noresult(ctx, "dummy"); (void) duk_peval_string(ctx, "dummy"); (void) duk_peval(ctx); (void) duk_pnew(ctx, 0); (void) duk_pop_2(ctx); (void) duk_pop_3(ctx); (void) duk_pop_n(ctx, 0); (void) duk_pop(ctx); (void) duk_push_array(ctx); (void) duk_push_boolean(ctx, 0); (void) duk_push_buffer_object(ctx, 0, 0, 0, 0); (void) duk_push_buffer(ctx, 0, 0); (void) duk_push_c_function(ctx, NULL, 0); (void) duk_push_c_lightfunc(ctx, NULL, 0, 0, 0); (void) duk_push_context_dump(ctx); (void) duk_push_current_function(ctx); (void) duk_push_current_thread(ctx); (void) duk_push_dynamic_buffer(ctx, 0); (void) duk_push_error_object_va(ctx, 0, NULL, NULL); (void) duk_push_error_object(ctx, 0, "dummy"); (void) duk_push_external_buffer(ctx); (void) duk_push_false(ctx); (void) duk_push_fixed_buffer(ctx, 0); (void) duk_push_global_object(ctx); (void) duk_push_global_stash(ctx); (void) duk_push_heap_stash(ctx); (void) duk_push_heapptr(ctx, NULL); (void) duk_push_int(ctx, 0); (void) duk_push_lstring(ctx, "dummy", 0); (void) duk_push_nan(ctx); (void) duk_push_null(ctx); (void) duk_push_number(ctx, 0.0); (void) duk_push_object(ctx); (void) duk_push_pointer(ctx, NULL); (void) duk_push_sprintf(ctx, "dummy"); (void) duk_push_string(ctx, "dummy"); (void) duk_push_this(ctx); (void) duk_push_thread_new_globalenv(ctx); (void) duk_push_thread_stash(ctx, NULL); (void) duk_push_thread(ctx); (void) duk_push_true(ctx); (void) duk_push_uint(ctx, 0); (void) duk_push_undefined(ctx); (void) duk_push_vsprintf(ctx, "dummy", NULL); (void) duk_put_function_list(ctx, 0, NULL); (void) duk_put_global_string(ctx, NULL); (void) duk_put_number_list(ctx, 0, NULL); (void) duk_put_prop_index(ctx, 0, 0); (void) duk_put_prop_string(ctx, 0, "dummy"); (void) duk_put_prop(ctx, 0); (void) duk_realloc_raw(ctx, NULL, 0); (void) duk_realloc(ctx, NULL, 0); (void) duk_remove(ctx, 0); (void) duk_replace(ctx, 0); (void) duk_require_boolean(ctx, 0); (void) duk_require_buffer_data(ctx, 0, NULL); (void) duk_require_buffer(ctx, 0, NULL); (void) duk_require_c_function(ctx, 0); (void) duk_require_callable(ctx, 0); (void) duk_require_context(ctx, 0); (void) duk_require_function(ctx, 0); (void) duk_require_heapptr(ctx, 0); (void) duk_require_int(ctx, 0); (void) duk_require_lstring(ctx, 0, NULL); (void) duk_require_normalize_index(ctx, 0); (void) duk_require_null(ctx, 0); (void) duk_require_number(ctx, 0); (void) duk_require_object_coercible(ctx, 0); (void) duk_require_pointer(ctx, 0); (void) duk_require_stack_top(ctx, 0); (void) duk_require_stack(ctx, 0); (void) duk_require_string(ctx, 0); (void) duk_require_top_index(ctx); (void) duk_require_type_mask(ctx, 0, 0); (void) duk_require_uint(ctx, 0); (void) duk_require_undefined(ctx, 0); (void) duk_require_valid_index(ctx, 0); (void) duk_resize_buffer(ctx, 0, 0); (void) duk_safe_call(ctx, NULL, NULL, 0, 0); (void) duk_safe_to_lstring(ctx, 0, NULL); (void) duk_safe_to_string(ctx, 0); (void) duk_set_finalizer(ctx, 0); (void) duk_set_global_object(ctx); (void) duk_set_magic(ctx, 0, 0); (void) duk_set_prototype(ctx, 0); (void) duk_set_top(ctx, 0); (void) duk_steal_buffer(ctx, 0, NULL); (void) duk_strict_equals(ctx, 0, 0); (void) duk_substring(ctx, 0, 0, 0); (void) duk_swap_top(ctx, 0); (void) duk_swap(ctx, 0, 0); (void) duk_throw(ctx); (void) duk_to_boolean(ctx, 0); (void) duk_to_buffer(ctx, 0, NULL); (void) duk_to_defaultvalue(ctx, 0, 0); (void) duk_to_dynamic_buffer(ctx, 0, NULL); (void) duk_to_fixed_buffer(ctx, 0, NULL); (void) duk_to_int32(ctx, 0); (void) duk_to_int(ctx, 0); (void) duk_to_lstring(ctx, 0, NULL); (void) duk_to_null(ctx, 0); (void) duk_to_number(ctx, 0); (void) duk_to_object(ctx, 0); (void) duk_to_pointer(ctx, 0); (void) duk_to_primitive(ctx, 0, 0); (void) duk_to_string(ctx, 0); (void) duk_to_uint16(ctx, 0); (void) duk_to_uint32(ctx, 0); (void) duk_to_uint(ctx, 0); (void) duk_to_undefined(ctx, 0); (void) duk_trim(ctx, 0); (void) duk_xcopy_top(ctx, NULL, 0); (void) duk_xmove_top(ctx, NULL, 0); printf("never here\n"); fflush(stdout); return 0; }
static duk_ret_t duk__require(duk_context *ctx) { const char *str_req_id; /* requested identifier */ const char *str_mod_id; /* require.id of current module */ duk_int_t pcall_rc; /* NOTE: we try to minimize code size by avoiding unnecessary pops, * so the stack looks a bit cluttered in this function. DUK__ASSERT_TOP() * assertions are used to ensure stack configuration is correct at each * step. */ /* * Resolve module identifier into canonical absolute form. */ str_req_id = duk_require_string(ctx, DUK__IDX_REQUESTED_ID); duk_push_current_function(ctx); duk_get_prop_string(ctx, -1, "id"); str_mod_id = duk_get_string(ctx, DUK__IDX_REQUIRE_ID); /* ignore non-strings */ duk__resolve_module_id(ctx, str_req_id, str_mod_id); str_req_id = NULL; str_mod_id = NULL; /* [ requested_id require require.id resolved_id last_comp ] */ DUK__ASSERT_TOP(ctx, DUK__IDX_LASTCOMP + 1); /* * Cached module check. * * If module has been loaded or its loading has already begun without * finishing, return the same cached value (module.exports). The * value is registered when module load starts so that circular * references can be supported to some extent. */ duk_push_global_stash(ctx); duk_get_prop_string(ctx, -1, "\xff" "module:Duktape"); duk_remove(ctx, -2); /* Lookup stashed, original 'Duktape' object. */ duk_get_prop_string(ctx, DUK__IDX_DUKTAPE, "modLoaded"); /* Duktape.modLoaded */ duk_require_type_mask(ctx, DUK__IDX_MODLOADED, DUK_TYPE_MASK_OBJECT); DUK__ASSERT_TOP(ctx, DUK__IDX_MODLOADED + 1); duk_dup(ctx, DUK__IDX_RESOLVED_ID); if (duk_get_prop(ctx, DUK__IDX_MODLOADED)) { /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded Duktape.modLoaded[id] ] */ duk_get_prop_string(ctx, -1, "exports"); /* return module.exports */ return 1; } DUK__ASSERT_TOP(ctx, DUK__IDX_UNDEFINED + 1); /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined ] */ /* * Module not loaded (and loading not started previously). * * Create a new require() function with 'id' set to resolved ID * of module being loaded. Also create 'exports' and 'module' * tables but don't register exports to the loaded table yet. * We don't want to do that unless the user module search callbacks * succeeds in finding the module. */ /* Fresh require: require.id is left configurable (but not writable) * so that is not easy to accidentally tweak it, but it can still be * done with Object.defineProperty(). * * XXX: require.id could also be just made non-configurable, as there * is no practical reason to touch it (at least from Ecmascript code). */ duk_push_c_function(ctx, duk__require, 1 /*nargs*/); duk_push_string(ctx, "name"); duk_push_string(ctx, "require"); duk_def_prop(ctx, DUK__IDX_FRESH_REQUIRE, DUK_DEFPROP_HAVE_VALUE); /* not writable, not enumerable, not configurable */ duk_push_string(ctx, "id"); duk_dup(ctx, DUK__IDX_RESOLVED_ID); duk_def_prop(ctx, DUK__IDX_FRESH_REQUIRE, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_SET_CONFIGURABLE); /* a fresh require() with require.id = resolved target module id */ /* Module table: * - module.exports: initial exports table (may be replaced by user) * - module.id is non-writable and non-configurable, as the CommonJS * spec suggests this if possible * - module.filename: not set, defaults to resolved ID if not explicitly * set by modSearch() (note capitalization, not .fileName, matches Node.js) * - module.name: not set, defaults to last component of resolved ID if * not explicitly set by modSearch() */ duk_push_object(ctx); /* exports */ duk_push_object(ctx); /* module */ duk_push_string(ctx, "exports"); duk_dup(ctx, DUK__IDX_EXPORTS); duk_def_prop(ctx, DUK__IDX_MODULE, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_SET_WRITABLE | DUK_DEFPROP_SET_CONFIGURABLE); /* module.exports = exports */ duk_push_string(ctx, "id"); duk_dup(ctx, DUK__IDX_RESOLVED_ID); /* resolved id: require(id) must return this same module */ duk_def_prop(ctx, DUK__IDX_MODULE, DUK_DEFPROP_HAVE_VALUE); /* module.id = resolved_id; not writable, not enumerable, not configurable */ duk_compact(ctx, DUK__IDX_MODULE); /* module table remains registered to modLoaded, minimize its size */ DUK__ASSERT_TOP(ctx, DUK__IDX_MODULE + 1); /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module ] */ /* Register the module table early to modLoaded[] so that we can * support circular references even in modSearch(). If an error * is thrown, we'll delete the reference. */ duk_dup(ctx, DUK__IDX_RESOLVED_ID); duk_dup(ctx, DUK__IDX_MODULE); duk_put_prop(ctx, DUK__IDX_MODLOADED); /* Duktape.modLoaded[resolved_id] = module */ /* * Call user provided module search function and build the wrapped * module source code (if necessary). The module search function * can be used to implement pure Ecmacsript, pure C, and mixed * Ecmascript/C modules. * * The module search function can operate on the exports table directly * (e.g. DLL code can register values to it). It can also return a * string which is interpreted as module source code (if a non-string * is returned the module is assumed to be a pure C one). If a module * cannot be found, an error must be thrown by the user callback. * * Because Duktape.modLoaded[] already contains the module being * loaded, circular references for C modules should also work * (although expected to be quite rare). */ duk_push_string(ctx, "(function(require,exports,module){"); /* Duktape.modSearch(resolved_id, fresh_require, exports, module). */ duk_get_prop_string(ctx, DUK__IDX_DUKTAPE, "modSearch"); /* Duktape.modSearch */ duk_dup(ctx, DUK__IDX_RESOLVED_ID); duk_dup(ctx, DUK__IDX_FRESH_REQUIRE); duk_dup(ctx, DUK__IDX_EXPORTS); duk_dup(ctx, DUK__IDX_MODULE); /* [ ... Duktape.modSearch resolved_id last_comp fresh_require exports module ] */ pcall_rc = duk_pcall(ctx, 4 /*nargs*/); /* -> [ ... source ] */ DUK__ASSERT_TOP(ctx, DUK__IDX_MODULE + 3); if (pcall_rc != DUK_EXEC_SUCCESS) { /* Delete entry in Duktape.modLoaded[] and rethrow. */ goto delete_rethrow; } /* If user callback did not return source code, module loading * is finished (user callback initialized exports table directly). */ if (!duk_is_string(ctx, -1)) { /* User callback did not return source code, so module loading * is finished: just update modLoaded with final module.exports * and we're done. */ goto return_exports; } /* Finish the wrapped module source. Force module.filename as the * function .fileName so it gets set for functions defined within a * module. This also ensures loggers created within the module get * the module ID (or overridden filename) as their default logger name. * (Note capitalization: .filename matches Node.js while .fileName is * used elsewhere in Duktape.) */ duk_push_string(ctx, "})"); duk_concat(ctx, 3); if (!duk_get_prop_string(ctx, DUK__IDX_MODULE, "filename")) { /* module.filename for .fileName, default to resolved ID if * not present. */ duk_pop(ctx); duk_dup(ctx, DUK__IDX_RESOLVED_ID); } pcall_rc = duk_pcompile(ctx, DUK_COMPILE_EVAL); if (pcall_rc != DUK_EXEC_SUCCESS) { goto delete_rethrow; } pcall_rc = duk_pcall(ctx, 0); /* -> eval'd function wrapper (not called yet) */ if (pcall_rc != DUK_EXEC_SUCCESS) { goto delete_rethrow; } /* Module has now evaluated to a wrapped module function. Force its * .name to match module.name (defaults to last component of resolved * ID) so that it is shown in stack traces too. Note that we must not * introduce an actual name binding into the function scope (which is * usually the case with a named function) because it would affect the * scope seen by the module and shadow accesses to globals of the same name. * This is now done by compiling the function as anonymous and then forcing * its .name without setting a "has name binding" flag. */ duk_push_string(ctx, "name"); if (!duk_get_prop_string(ctx, DUK__IDX_MODULE, "name")) { /* module.name for .name, default to last component if * not present. */ duk_pop(ctx); duk_dup(ctx, DUK__IDX_LASTCOMP); } duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE); /* * Call the wrapped module function. * * Use a protected call so that we can update Duktape.modLoaded[resolved_id] * even if the module throws an error. */ /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module mod_func ] */ DUK__ASSERT_TOP(ctx, DUK__IDX_MODULE + 2); duk_dup(ctx, DUK__IDX_EXPORTS); /* exports (this binding) */ duk_dup(ctx, DUK__IDX_FRESH_REQUIRE); /* fresh require (argument) */ duk_get_prop_string(ctx, DUK__IDX_MODULE, "exports"); /* relookup exports from module.exports in case it was changed by modSearch */ duk_dup(ctx, DUK__IDX_MODULE); /* module (argument) */ DUK__ASSERT_TOP(ctx, DUK__IDX_MODULE + 6); /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module mod_func exports fresh_require exports module ] */ pcall_rc = duk_pcall_method(ctx, 3 /*nargs*/); if (pcall_rc != DUK_EXEC_SUCCESS) { /* Module loading failed. Node.js will forget the module * registration so that another require() will try to load * the module again. Mimic that behavior. */ goto delete_rethrow; } /* [ requested_id require require.id resolved_id last_comp Duktape Duktape.modLoaded undefined fresh_require exports module result(ignored) ] */ DUK__ASSERT_TOP(ctx, DUK__IDX_MODULE + 2); /* fall through */ return_exports: duk_get_prop_string(ctx, DUK__IDX_MODULE, "exports"); duk_compact(ctx, -1); /* compact the exports table */ return 1; /* return module.exports */ delete_rethrow: duk_dup(ctx, DUK__IDX_RESOLVED_ID); duk_del_prop(ctx, DUK__IDX_MODLOADED); /* delete Duktape.modLoaded[resolved_id] */ duk_throw(ctx); /* rethrow original error */ return 0; /* not reachable */ }
//duk_int_t duk_pcall_method(duk_context *ctx, duk_idx_t nargs); duk_int_t aperl_duk_pcall_method(duk_context *ctx, duk_idx_t nargs) { duk_int_t ret = duk_pcall_method(ctx, nargs); return ret; }
static AJ_Status HandleMessage(duk_context* ctx, duk_idx_t ajIdx, AJ_Message* msg) { duk_idx_t msgIdx; uint8_t accessor = AJS_NOT_ACCESSOR; const char* func; AJ_Status status; uint8_t ldstate; #if !defined(AJS_CONSOLE_LOCKDOWN) status = AJS_GetLockdownState(&ldstate); if (status == AJ_OK && ldstate == AJS_CONSOLE_UNLOCKED) { status = AJS_ConsoleMsgHandler(ctx, msg); if (status != AJ_ERR_NO_MATCH) { if (status != AJ_OK) { AJ_WarnPrintf(("AJS_ConsoleMsgHandler returned %s\n", AJ_StatusText(status))); } return status; } } #endif /* * JOIN_SESSION replies are handled in the AllJoyn.js layer and don't get passed to JavaScript */ if (msg->msgId == AJ_REPLY_ID(AJ_METHOD_JOIN_SESSION)) { return AJS_HandleJoinSessionReply(ctx, msg); } /* * Nothing more to do if the AllJoyn module was not loaded */ if (ajIdx < 0) { return AJ_OK; } /* * Let the bases services layer take a look at the message */ status = AJS_ServicesMsgHandler(msg); if (status != AJ_ERR_NO_MATCH) { if (status != AJ_OK) { AJ_WarnPrintf(("AJS_ServicesMsgHandler returned %s\n", AJ_StatusText(status))); } return status; } /* * Push the appropriate callback function onto the duktape stack */ if (msg->hdr->msgType == AJ_MSG_SIGNAL) { /* * About announcements and found name signal get special handling */ if (msg->msgId == AJ_SIGNAL_ABOUT_ANNOUNCE) { return AJS_AboutAnnouncement(ctx, msg); } if (msg->msgId == AJ_SIGNAL_FOUND_ADV_NAME) { return AJS_FoundAdvertisedName(ctx, msg); } if ((msg->msgId == AJ_SIGNAL_SESSION_LOST) || (msg->msgId == AJ_SIGNAL_SESSION_LOST_WITH_REASON)) { if (AJS_DebuggerIsAttached()) { msg = AJS_CloneAndCloseMessage(ctx, msg); } return AJS_SessionLost(ctx, msg); } func = "onSignal"; duk_get_prop_string(ctx, ajIdx, func); } else if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) { accessor = IsPropAccessor(msg); switch (accessor) { case AJS_NOT_ACCESSOR: func = "onMethodCall"; break; case AJ_PROP_GET: func = "onPropGet"; break; case AJ_PROP_SET: func = "onPropSet"; break; case AJ_PROP_GET_ALL: func = "onPropGetAll"; break; default: return AJ_ERR_INVALID; } duk_get_prop_string(ctx, ajIdx, func); } else { func = "onReply"; AJS_GetGlobalStashObject(ctx, func); if (duk_is_object(ctx, -1)) { duk_get_prop_index(ctx, -1, msg->replySerial); duk_swap_top(ctx, -2); /* * Clear the onReply entry */ duk_del_prop_index(ctx, -1, msg->replySerial); duk_pop(ctx); } } /* * Skip if there is no function to call. */ if (!duk_is_function(ctx, -1)) { if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) { AJ_Message error; AJ_WarnPrintf(("%s: not registered - rejecting message\n", func)); AJ_MarshalErrorMsg(msg, &error, AJ_ErrRejected); status = AJ_DeliverMsg(&error); } else { AJ_WarnPrintf(("%s: not registered - ignoring message\n", func)); status = AJ_OK; } duk_pop(ctx); return status; } /* * Opens up a stack entry above the function */ duk_dup_top(ctx); msgIdx = AJS_UnmarshalMessage(ctx, msg, accessor); AJ_ASSERT(msgIdx == (ajIdx + 3)); /* * Save the message object on the stack */ duk_copy(ctx, msgIdx, -3); /* * Special case for GET prop so we can get the signature for marshalling the result */ if (accessor == AJS_NOT_ACCESSOR) { status = AJS_UnmarshalMsgArgs(ctx, msg); } else { status = AJS_UnmarshalPropArgs(ctx, msg, accessor, msgIdx); } if (status == AJ_OK) { duk_idx_t numArgs = duk_get_top(ctx) - msgIdx - 1; /* * If attached, the debugger will begin to unmarshal a message when the * method handler is called, therefore it must be cloned-and-closed now. */ if (AJS_DebuggerIsAttached()) { msg = AJS_CloneAndCloseMessage(ctx, msg); } if (duk_pcall_method(ctx, numArgs) != DUK_EXEC_SUCCESS) { const char* err = duk_safe_to_string(ctx, -1); AJ_ErrPrintf(("%s: %s\n", func, err)); /* * Generate an error reply if this was a method call */ if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) { duk_push_c_lightfunc(ctx, AJS_MethodCallError, 1, 0, 0); duk_insert(ctx, -3); (void)duk_pcall_method(ctx, 1); } } } /* * Cleanup stack back to the AJ object */ duk_set_top(ctx, ajIdx + 1); return status; }
/* * Call a JS method/function. * Usage: call token method this ?{arg ?type?}? ... * Return value: the result of the method call coerced to string. * Side effects: may change the Duktape interpreter heap. */ static int CallMethod_Cmd(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { int i; int list_length; int tableIndex; int int_value; double double_value; duk_context *ctx; duk_int_t duk_result; Tcl_Obj *value; Tcl_Obj *type; static const char *types[] = { "boolean", "nan", "null", "number", "string", "undefined", (char *)NULL }; enum types { TYPE_BOOLEAN, TYPE_NAN, TYPE_NULL, TYPE_NUMBER, TYPE_STRING, TYPE_UNDEFINED }; if (objc < 4) { Tcl_WrongNumArgs(interp, 1, objv, USAGE_CALL_METHOD); return TCL_ERROR; } ctx = parse_id(cdata, interp, objv[1], 0); if (ctx == NULL) { return TCL_ERROR; } /* Eval the function name and "this" to put them on the stack. */ for (i = 2; i < 4; i++) { duk_result = duk_peval_string(ctx, Tcl_GetString(objv[i])); if (duk_result != 0) { Tcl_SetObjResult(interp, Tcl_NewStringObj( duk_safe_to_string(ctx, -1), -1)); duk_pop(ctx); return TCL_ERROR; } } /* Push the arguments. */ for (i = 4; i < objc; i++) { if (Tcl_ListObjIndex(interp, objv[i], 0, &value) != TCL_OK) { return TCL_ERROR; } if (Tcl_ListObjLength(interp, objv[i], &list_length) != TCL_OK) { return TCL_ERROR; } if (list_length == 2) { if (Tcl_ListObjIndex(interp, objv[i], 1, &type) != TCL_OK) { return TCL_ERROR; } if (Tcl_GetIndexFromObj(interp, type, types, "type", 0, &tableIndex) != TCL_OK) { return TCL_ERROR; } } else if (list_length == 1) { tableIndex = TYPE_STRING; } else { Tcl_SetObjResult(interp, Tcl_NewStringObj(ERROR_ARG_LENGTH, -1)); return TCL_ERROR; } switch ((enum types) tableIndex) { case TYPE_BOOLEAN: if (Tcl_GetIntFromObj(interp, value, &int_value) != TCL_OK) { return TCL_ERROR; } duk_push_boolean(ctx, int_value); break; case TYPE_NAN: duk_push_nan(ctx); break; case TYPE_NULL: duk_push_null(ctx); break; case TYPE_NUMBER: if (Tcl_GetDoubleFromObj(interp, value, &double_value) != TCL_OK) { return TCL_ERROR; } duk_push_number(ctx, double_value); break; case TYPE_UNDEFINED: duk_push_undefined(ctx); break; case TYPE_STRING: default: duk_push_string(ctx, Tcl_GetString(value)); break; } } duk_result = duk_pcall_method(ctx, objc - 4); Tcl_SetObjResult(interp, Tcl_NewStringObj(duk_safe_to_string(ctx, -1), -1)); duk_pop(ctx); if (duk_result == 0) { return TCL_OK; } else { return TCL_ERROR; } }
static duk_ret_t duv_main(duk_context *ctx) { duk_require_string(ctx, 0); { duk_push_global_object(ctx); duk_dup(ctx, -1); duk_put_prop_string(ctx, -2, "global"); duk_push_boolean(ctx, 1); duk_put_prop_string(ctx, -2, "dukluv"); // [global] // Load duv module into global uv duk_push_c_function(ctx, dukopen_uv, 0); duk_call(ctx, 0); // [global obj] duk_put_prop_string(ctx, -2, "uv"); // Replace the module loader with Duktape 2.x polyfill. duk_get_prop_string(ctx, -1, "Duktape"); duk_del_prop_string(ctx, -1, "modSearch"); duk_push_c_function(ctx, duv_mod_compile, 1); duk_put_prop_string(ctx, -2, "modCompile"); duk_push_c_function(ctx, duv_mod_resolve, 1); duk_put_prop_string(ctx, -2, "modResolve"); duk_push_c_function(ctx, duv_mod_load, 0); duk_put_prop_string(ctx, -2, "modLoad"); duk_push_c_function(ctx, duv_loadlib, 2); duk_put_prop_string(ctx, -2, "loadlib"); duk_pop(ctx); // Put in some quick globals to test things. duk_push_c_function(ctx, duv_path_join, DUK_VARARGS); duk_put_prop_string(ctx, -2, "pathJoin"); duk_push_c_function(ctx, duv_loadfile, 1); duk_put_prop_string(ctx, -2, "loadFile"); // require.call({id:uv.cwd()+"/main.c"}, path); duk_push_c_function(ctx, duv_require, 1); { // Store this require function in the module prototype duk_push_global_stash(ctx); duk_push_object(ctx); duk_dup(ctx, -3); duk_put_prop_string(ctx, -2, "require"); duk_put_prop_string(ctx, -2, "modulePrototype"); duk_pop(ctx); } } { duk_push_object(ctx); duk_push_c_function(ctx, duv_cwd, 0); duk_call(ctx, 0); duk_push_string(ctx, "/main.c"); duk_concat(ctx, 2); duk_put_prop_string(ctx, -2, "id"); } duk_dup(ctx, 0); //[arg.js global duv_require obj arg.js] duk_int_t ret = duk_pcall_method(ctx, 1); if (ret) { duv_dump_error(ctx, -1); return 0; } return 0; }