static duk_ret_t test_func(duk_context *ctx) { if (ctx) { printf("dummy - return here\n"); fflush(stdout); return 0; } /* Up-to-date for Duktape 0.12.0, alphabetical order: * $ cd website/api; ls *.txt */ (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_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_file(ctx, 0, "dummy"); (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_copy(ctx, 0, 0); (void) duk_create_heap_default(); (void) duk_create_heap(NULL, NULL, NULL, NULL, NULL); (void) duk_decode_string(ctx, 0, NULL, NULL); (void) duk_del_prop_index(ctx, 0, 0); (void) duk_del_prop_string(ctx, 0, "dummy"); (void) duk_del_prop(ctx, 0); (void) duk_del_var(ctx); (void) duk_destroy_heap(ctx); (void) duk_dump_context_stderr(ctx); (void) duk_dump_context_stdout(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(ctx, 0, "dummy"); /* (void) cast won't work without variadic macros */ (void) duk_eval_file_noresult(ctx, "dummy"); (void) duk_eval_file(ctx, "dummy"); (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, 0, "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(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_finalizer(ctx, 0); (void) duk_get_global_string(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_get_var(ctx); (void) duk_has_prop_index(ctx, 0, 0); (void) duk_has_prop_string(ctx, 0, "dummy"); (void) duk_has_prop(ctx, 0); (void) duk_has_var(ctx); (void) duk_hex_decode(ctx, 0); (void) duk_hex_encode(ctx, 0); (void) duk_insert(ctx, 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_fixed_buffer(ctx, 0); (void) duk_is_function(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_strict_call(ctx); (void) duk_is_string(ctx, 0); (void) duk_is_thread(ctx, 0); (void) duk_is_undefined(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_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_file(ctx, 0, "dummy"); (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_file_noresult(ctx, "dummy"); (void) duk_peval_file(ctx, "dummy"); (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_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(ctx, 0, 0); (void) duk_push_c_function(ctx, NULL, 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(ctx, 0, "dummy"); (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_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_file(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_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_put_var(ctx); (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(ctx, 0, NULL); (void) duk_require_c_function(ctx, 0); (void) duk_require_context(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, 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_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; }
bool StyleContext::parseStyleResult(StyleParamKey _key, StyleParam::Value& _val) const { _val = none_type{}; if (duk_is_string(m_ctx, -1)) { std::string value(duk_get_string(m_ctx, -1)); _val = StyleParam::parseString(_key, value); } else if (duk_is_boolean(m_ctx, -1)) { bool value = duk_get_boolean(m_ctx, -1); switch (_key) { case StyleParamKey::visible: _val = value; break; case StyleParamKey::extrude: _val = value ? glm::vec2(NAN, NAN) : glm::vec2(0.0f, 0.0f); break; default: break; } } else if (duk_is_array(m_ctx, -1)) { duk_get_prop_string(m_ctx, -1, "length"); int len = duk_get_int(m_ctx, -1); duk_pop(m_ctx); switch (_key) { case StyleParamKey::extrude: { if (len != 2) { logMsg("Warning: Wrong array size for extrusion: '%d'.\n", len); break; } duk_get_prop_index(m_ctx, -1, 0); double v1 = duk_get_number(m_ctx, -1); duk_pop(m_ctx); duk_get_prop_index(m_ctx, -1, 1); double v2 = duk_get_number(m_ctx, -1); duk_pop(m_ctx); _val = glm::vec2(v1, v2); break; } case StyleParamKey::color: case StyleParamKey::outline_color: case StyleParamKey::font_fill: case StyleParamKey::font_stroke: case StyleParamKey::font_stroke_color: { if (len < 3 || len > 4) { logMsg("Warning: Wrong array size for color: '%d'.\n", len); break; } duk_get_prop_index(m_ctx, -1, 0); double r = duk_get_number(m_ctx, -1); duk_pop(m_ctx); duk_get_prop_index(m_ctx, -1, 1); double g = duk_get_number(m_ctx, -1); duk_pop(m_ctx); duk_get_prop_index(m_ctx, -1, 2); double b = duk_get_number(m_ctx, -1); duk_pop(m_ctx); double a = 1.0; if (len == 4) { duk_get_prop_index(m_ctx, -1, 3); a = duk_get_number(m_ctx, -1); duk_pop(m_ctx); } _val = (((uint32_t)(255.0 * a) & 0xff) << 24) | (((uint32_t)(255.0 * r) & 0xff)<< 16) | (((uint32_t)(255.0 * g) & 0xff)<< 8) | (((uint32_t)(255.0 * b) & 0xff)); break; } default: break; } } else if (duk_is_number(m_ctx, -1)) { switch (_key) { case StyleParamKey::width: case StyleParamKey::outline_width: case StyleParamKey::font_stroke_width: { double v = duk_get_number(m_ctx, -1); _val = static_cast<float>(v); break; } case StyleParamKey::order: case StyleParamKey::priority: case StyleParamKey::color: case StyleParamKey::outline_color: case StyleParamKey::font_fill: case StyleParamKey::font_stroke: case StyleParamKey::font_stroke_color: { _val = static_cast<uint32_t>(duk_get_uint(m_ctx, -1)); break; } default: break; } } else { logMsg("Warning: Unhandled return type from Javascript function.\n"); } duk_pop(m_ctx); DUMP("parseStyleResult\n"); return !_val.is<none_type>(); }
void AssignAttributeValue(duk_context* ctx, duk_idx_t stackIndex, IAttribute* destAttr, AttributeChange::Type change) { if (!destAttr) return; switch (destAttr->TypeId()) { case IAttribute::BoolId: static_cast<Attribute<bool>*>(destAttr)->Set(duk_get_boolean(ctx, stackIndex) ? true : false, change); break; case IAttribute::IntId: static_cast<Attribute<int>*>(destAttr)->Set((int)duk_get_number(ctx, stackIndex), change); break; case IAttribute::UIntId: static_cast<Attribute<uint>*>(destAttr)->Set((uint)duk_get_number(ctx, stackIndex), change); break; case IAttribute::RealId: static_cast<Attribute<float>*>(destAttr)->Set((float)duk_get_number(ctx, stackIndex), change); break; case IAttribute::StringId: static_cast<Attribute<String>*>(destAttr)->Set(String(duk_get_string(ctx, stackIndex)), change); break; case IAttribute::Float2Id: if (duk_is_object(ctx, stackIndex) && strcmp(GetValueObjectType(ctx, stackIndex), float2_ID) == 0) static_cast<Attribute<float2>*>(destAttr)->Set(*GetValueObject<float2>(ctx, stackIndex, nullptr), change); break; case IAttribute::Float3Id: if (duk_is_object(ctx, stackIndex) && strcmp(GetValueObjectType(ctx, stackIndex), float3_ID) == 0) static_cast<Attribute<float3>*>(destAttr)->Set(*GetValueObject<float3>(ctx, stackIndex, nullptr), change); break; case IAttribute::Float4Id: if (duk_is_object(ctx, stackIndex) && strcmp(GetValueObjectType(ctx, stackIndex), float4_ID) == 0) static_cast<Attribute<float4>*>(destAttr)->Set(*GetValueObject<float4>(ctx, stackIndex, nullptr), change); break; case IAttribute::QuatId: if (duk_is_object(ctx, stackIndex) && strcmp(GetValueObjectType(ctx, stackIndex), Quat_ID) == 0) static_cast<Attribute<Quat>*>(destAttr)->Set(*GetValueObject<Quat>(ctx, stackIndex, nullptr), change); break; case IAttribute::TransformId: if (duk_is_object(ctx, stackIndex) && strcmp(GetValueObjectType(ctx, stackIndex), Transform_ID) == 0) static_cast<Attribute<Transform>*>(destAttr)->Set(*GetValueObject<Transform>(ctx, stackIndex, nullptr), change); break; case IAttribute::AssetReferenceId: if (duk_is_object(ctx, stackIndex) && strcmp(GetValueObjectType(ctx, stackIndex), AssetReference_ID) == 0) static_cast<Attribute<AssetReference>*>(destAttr)->Set(*GetValueObject<AssetReference>(ctx, stackIndex, nullptr), change); break; case IAttribute::AssetReferenceListId: if (duk_is_object(ctx, stackIndex) && strcmp(GetValueObjectType(ctx, stackIndex), AssetReferenceList_ID) == 0) static_cast<Attribute<AssetReferenceList>*>(destAttr)->Set(*GetValueObject<AssetReferenceList>(ctx, stackIndex, nullptr), change); // Also allow assigning a single AssetReference to an AssetReferenceList else if (duk_is_object(ctx, stackIndex) && strcmp(GetValueObjectType(ctx, stackIndex), AssetReference_ID) == 0) { const AssetReference& ref = *GetValueObject<AssetReference>(ctx, stackIndex, nullptr); AssetReferenceList list; list.type = ref.type; list.Append(ref); static_cast<Attribute<AssetReferenceList>*>(destAttr)->Set(list); } break; case IAttribute::EntityReferenceId: if (duk_is_object(ctx, stackIndex) && strcmp(GetValueObjectType(ctx, stackIndex), EntityReference_ID) == 0) static_cast<Attribute<EntityReference>*>(destAttr)->Set(*GetValueObject<EntityReference>(ctx, stackIndex, nullptr), change); break; } }
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 */ }
const char* Context::getString(index_t i) { return duk_get_string(m_handle, i); }
//----------------------------------------------------------------------------- void AdTiledManager::Load(const char* pName) { Unload(); char pFN[FILENAME_MAX]; sprintf(pFN, MAP_LOCATION, pName); duk_context* ctx = s_pJSCtx; duk_push_string_file(ctx, pFN); duk_json_decode(ctx, -1); if(!duk_is_object(ctx, -1)) { fprintf(stderr, "NOTE: Failed parse %s.json!\n", pName); return; } duk_get_prop_string(ctx, -1, "width"); m_iWidth = duk_to_int(ctx, -1); duk_pop(ctx); duk_get_prop_string(ctx, -1, "height"); m_iHeight = duk_to_int(ctx, -1); duk_pop(ctx); duk_get_prop_string(ctx, -1, "layers"); if(duk_is_array(ctx, -1)) { m_nLayers = duk_get_length(ctx, -1); m_pIndices = (int**) calloc(m_nLayers, sizeof(int*)); for(int j=0; j<m_nLayers; ++j) { duk_get_prop_index(ctx, -1, j); // NOTE: if this is a tile layer grab the indices, otherwise set // to NULL duk_get_prop_string(ctx, -1, "data"); if(duk_is_array(ctx, -1)) { int size = duk_get_length(ctx, -1); if((m_iWidth*m_iHeight) == size) { m_pIndices[j] = (int*) malloc(size*sizeof(int)); for(int i=0; i<size; ++i) { duk_get_prop_index(ctx, -1, i); m_pIndices[j][i] = duk_to_int(ctx, -1); duk_pop(ctx); } } } duk_pop(ctx); // NOTE: loop over the map entities and add them to the array of // entities duk_get_prop_string(ctx, -1, "name"); if(duk_is_string(ctx, -1)) { if(!strcmp(duk_get_string(ctx, -1), "entities")) { // NOTE: -2 referring back to object and not the string duk_get_prop_string(ctx, -2, "objects"); if(duk_is_array(ctx, -1)) { int size = duk_get_length(ctx, -1); for(int e=0; e<size; ++e) { duk_get_prop_index(ctx, -1, e); duk_get_prop_string(ctx, -1, "type"); const char* type = duk_get_string(ctx, -1); AdEntity* pEnt = NULL; if(!strcmp(type, "NPC-TEST")) { duk_pop(ctx); pEnt = new NpcTree0(); pEnt->Load(ctx); } else if(!strcmp(type, "NPC-RON")) { duk_pop(ctx); pEnt = new NpcRon(); pEnt->Load(ctx); } else if(!strcmp(type, "NPC-PYTHON")) { duk_pop(ctx); pEnt = new NpcPython(); pEnt->Load(ctx); } else if(!strcmp(type, "NPC-BURRITO")) { duk_pop(ctx); pEnt = new NpcBurrito(); pEnt->Load(ctx); } else if(!strcmp(type, "NPC-JAVALS")) { duk_pop(ctx); pEnt = new NpcJavals(); pEnt->Load(ctx); } else if(!strcmp(type, "NPC-GWEN")) { duk_pop(ctx); pEnt = new NpcGwen(); pEnt->Load(ctx); } else if(!strcmp(type, "NPC-AVOCADO")) { duk_pop(ctx); pEnt = new NpcAvocado(); pEnt->Load(ctx); } else if(!strcmp(type, "NPC-PUZZLE_PIECE")) { duk_pop(ctx); pEnt = new NpcPuzzlePiece(); pEnt->Load(ctx); } else if(!strcmp(type, "NPC-PUZZLE_PIECE2")) { duk_pop(ctx); pEnt = new NpcPuzzlePiece2(); pEnt->Load(ctx); } else if(!strcmp(type, "NPC-STATUE_BASE")) { duk_pop(ctx); pEnt = new NpcStatueBase(); pEnt->Load(ctx); } else if( !strcmp(type, "LVL-UP-0") || !strcmp(type, "LVL-DOWN-0") || !strcmp(type, "LVL-LEFT-0") || !strcmp(type, "LVL-RIGHT-0") ) { duk_pop(ctx); pEnt = new AdEntity(); pEnt->Load(ctx); } else { duk_pop(ctx); } if(pEnt) { m_pEntities = (AdEntity**) realloc( m_pEntities, ++m_nEntities*sizeof(AdEntity*) ); m_pEntities[m_nEntities-1] = pEnt; } duk_pop(ctx); } } duk_pop(ctx); } } duk_pop_2(ctx); } } duk_pop_2(ctx); }
//const char *duk_get_string(duk_context *ctx, duk_idx_t index); const char *aperl_duk_get_string(duk_context *ctx, duk_idx_t index) { const char *ret = duk_get_string(ctx, index); return ret; }
/* load and eval a file */ int js_load_file(duk_context *ctx) { const char *file = duk_get_string(ctx, 0); duk_eval_file(ctx, file); return 0; }
static int duk__traceback_getter_helper(duk_context *ctx, int output_type) { duk_hthread *thr = (duk_hthread *) ctx; int idx_td; int i; const char *str_tailcalled = " tailcalled"; const char *str_strict = " strict"; const char *str_construct = " construct"; const char *str_prevyield = " preventsyield"; const char *str_directeval = " directeval"; const char *str_empty = ""; DUK_ASSERT_TOP(ctx, 0); /* fixed arg count */ duk_push_this(ctx); duk_get_prop_stridx(ctx, -1, DUK_STRIDX_TRACEDATA); idx_td = duk_get_top_index(ctx); duk_push_hstring_stridx(ctx, DUK_STRIDX_NEWLINE_TAB); duk_push_this(ctx); duk_to_string(ctx, -1); /* [ ... this tracedata sep ToString(this) ] */ /* FIXME: skip null filename? */ if (duk_check_type(ctx, idx_td, DUK_TYPE_OBJECT)) { int t; /* Current tracedata contains 2 entries per callstack entry. */ for (i = 0; ; i += 2) { int pc; int line; int flags; double d; const char *funcname; duk_hobject *h_func; duk_hstring *h_name; duk_hbuffer_fixed *pc2line; duk_require_stack(ctx, 5); duk_get_prop_index(ctx, idx_td, i); duk_get_prop_index(ctx, idx_td, i + 1); d = duk_to_number(ctx, -1); pc = (int) fmod(d, DUK_DOUBLE_2TO32); flags = (int) floor(d / DUK_DOUBLE_2TO32); t = duk_get_type(ctx, -2); if (t == DUK_TYPE_OBJECT) { /* * Ecmascript/native function call */ /* [ ... v1(func) v2(pc+flags) ] */ h_func = duk_get_hobject(ctx, -2); DUK_ASSERT(h_func != NULL); duk_get_prop_stridx(ctx, -2, DUK_STRIDX_NAME); duk_get_prop_stridx(ctx, -3, DUK_STRIDX_FILE_NAME); duk_get_prop_stridx(ctx, -4, DUK_STRIDX_INT_PC2LINE); if (duk_is_buffer(ctx, -1)) { pc2line = (duk_hbuffer_fixed *) duk_get_hbuffer(ctx, -1); DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) pc2line)); line = duk_hobject_pc2line_query(pc2line, (duk_uint_fast32_t) pc); } else { line = 0; } duk_pop(ctx); /* [ ... v1 v2 name filename ] */ if (output_type == DUK__OUTPUT_TYPE_FILENAME) { return 1; } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) { duk_push_int(ctx, line); return 1; } h_name = duk_get_hstring(ctx, -2); /* may be NULL */ funcname = (h_name == NULL || h_name == DUK_HTHREAD_STRING_EMPTY_STRING(thr)) ? "anon" : (const char *) DUK_HSTRING_GET_DATA(h_name); if (DUK_HOBJECT_HAS_NATIVEFUNCTION(h_func)) { duk_push_sprintf(ctx, "%s %s native%s%s%s%s%s", funcname, duk_get_string(ctx, -1), (flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty, (flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcalled : str_empty, (flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty, (flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty, (flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty); } else { duk_push_sprintf(ctx, "%s %s:%d%s%s%s%s%s", funcname, duk_get_string(ctx, -1), line, (flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty, (flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcalled : str_empty, (flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty, (flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty, (flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty); } duk_replace(ctx, -5); /* [ ... v1 v2 name filename str ] -> [ ... str v2 name filename ] */ duk_pop_n(ctx, 3); /* -> [ ... str ] */ } else if (t == DUK_TYPE_STRING) { /* * __FILE__ / __LINE__ entry, here 'pc' is line number directly. * Sometimes __FILE__ / __LINE__ is reported as the source for * the error (fileName, lineNumber), sometimes not. */ /* [ ... v1(filename) v2(line+flags) ] */ if (!(flags & DUK_TB_FLAG_NOBLAME_FILELINE)) { if (output_type == DUK__OUTPUT_TYPE_FILENAME) { duk_pop(ctx); return 1; } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) { duk_push_int(ctx, pc); return 1; } } duk_push_sprintf(ctx, "%s:%d", duk_get_string(ctx, -2), pc); duk_replace(ctx, -3); /* [ ... v1 v2 str ] -> [ ... str v2 ] */ duk_pop(ctx); /* -> [ ... str ] */ } else { /* unknown, ignore */ duk_pop_2(ctx); break; } } if (i >= DUK_USE_TRACEBACK_DEPTH * 2) { /* Possibly truncated; there is no explicit truncation * marker so this is the best we can do. */ duk_push_hstring_stridx(ctx, DUK_STRIDX_BRACKETED_ELLIPSIS); } } /* [ ... this tracedata sep ToString(this) str1 ... strN ] */ if (output_type != DUK__OUTPUT_TYPE_TRACEBACK) { return 0; } else { duk_join(ctx, duk_get_top(ctx) - (idx_td + 2) /*count, not including sep*/); return 1; } }
//----------------------------------------------------------------------------- void AdTiledManager::Load(const char* pName) { Unload(); char pFN[FILENAME_MAX]; sprintf(pFN, MAP_LOCATION, pName); duk_context* ctx = s_pJSCtx; duk_push_string_file(ctx, pFN); duk_json_decode(ctx, -1); if(!duk_is_object(ctx, -1)) { fprintf(stderr, "NOTE: Failed parse %s.json!\n", pName); return; } duk_get_prop_string(ctx, -1, "width"); m_iWidth = duk_to_int(ctx, -1); duk_pop(ctx); duk_get_prop_string(ctx, -1, "height"); m_iHeight = duk_to_int(ctx, -1); duk_pop(ctx); duk_get_prop_string(ctx, -1, "layers"); if(duk_is_array(ctx, -1)) { m_nLayers = duk_get_length(ctx, -1); m_pIndices = (int**) calloc(m_nLayers, sizeof(int*)); for(int j=0; j<m_nLayers; ++j) { duk_get_prop_index(ctx, -1, j); // NOTE: if this is a tile layer grab the indices, otherwise set // to NULL duk_get_prop_string(ctx, -1, "data"); if(duk_is_array(ctx, -1)) { int size = duk_get_length(ctx, -1); if((m_iWidth*m_iHeight) == size) { m_pIndices[j] = (int*) malloc(size*sizeof(int)); for(int i=0; i<size; ++i) { duk_get_prop_index(ctx, -1, i); m_pIndices[j][i] = duk_to_int(ctx, -1); duk_pop(ctx); } } } duk_pop(ctx); // NOTE: loop over the map entities and add them to the array of // entities duk_get_prop_string(ctx, -1, "name"); if(duk_is_string(ctx, -1)) { if(!strcmp(duk_get_string(ctx, -1), "entities")) { // NOTE: -2 referring back to object and not the string duk_get_prop_string(ctx, -2, "objects"); if(duk_is_array(ctx, -1)) { int size = duk_get_length(ctx, -1); for(int e=0; e<size; ++e) { duk_get_prop_index(ctx, -1, e); duk_get_prop_string(ctx, -1, "type"); const char* type = duk_get_string(ctx, -1); AdEntity* pEnt = NULL; if(!strcmp(type, "NPC-PLAYER")) { duk_pop(ctx); pEnt = new AdPlayer(); pEnt->Load(ctx); } else if(!strcmp(type, "NPC-TEST")) { duk_pop(ctx); pEnt = new AdMoveable(); pEnt->Load(ctx); } else if(!strcmp(type, "TEST")) { duk_pop(ctx); pEnt = new AdEntity(); pEnt->Load(ctx); } else { duk_pop(ctx); } if(pEnt) { m_pEntities = (AdEntity**) realloc( m_pEntities, ++m_nEntities*sizeof(AdEntity*) ); m_pEntities[m_nEntities-1] = pEnt; } duk_pop(ctx); } } duk_pop(ctx); } } duk_pop_2(ctx); } } duk_pop_2(ctx); }
DUK_LOCAL duk_ret_t duk__error_getter_helper(duk_context *ctx, duk_small_int_t output_type) { duk_hthread *thr = (duk_hthread *) ctx; duk_idx_t idx_td; duk_small_int_t i; /* traceback depth fits into 16 bits */ duk_small_int_t t; /* stack type fits into 16 bits */ duk_small_int_t count_func = 0; /* traceback depth ensures fits into 16 bits */ const char *str_tailcall = " tailcall"; const char *str_strict = " strict"; const char *str_construct = " construct"; const char *str_prevyield = " preventsyield"; const char *str_directeval = " directeval"; const char *str_empty = ""; DUK_ASSERT_TOP(ctx, 0); /* fixed arg count */ DUK_UNREF(thr); duk_push_this(ctx); duk_get_prop_stridx_short(ctx, -1, DUK_STRIDX_INT_TRACEDATA); idx_td = duk_get_top_index(ctx); duk_push_hstring_stridx(ctx, DUK_STRIDX_NEWLINE_4SPACE); duk_push_this(ctx); /* [ ... this tracedata sep this ] */ /* XXX: skip null filename? */ if (duk_check_type(ctx, idx_td, DUK_TYPE_OBJECT)) { /* Current tracedata contains 2 entries per callstack entry. */ for (i = 0; ; i += 2) { duk_int_t pc; duk_int_t line; duk_int_t flags; duk_double_t d; const char *funcname; const char *filename; duk_hobject *h_func; duk_hstring *h_name; duk_require_stack(ctx, 5); duk_get_prop_index(ctx, idx_td, i); duk_get_prop_index(ctx, idx_td, i + 1); d = duk_to_number_m1(ctx); pc = (duk_int_t) DUK_FMOD(d, DUK_DOUBLE_2TO32); flags = (duk_int_t) DUK_FLOOR(d / DUK_DOUBLE_2TO32); t = (duk_small_int_t) duk_get_type(ctx, -2); if (t == DUK_TYPE_OBJECT || t == DUK_TYPE_LIGHTFUNC) { /* * Ecmascript/native function call or lightfunc call */ count_func++; /* [ ... v1(func) v2(pc+flags) ] */ h_func = duk_get_hobject(ctx, -2); /* NULL for lightfunc */ /* These may be systematically omitted by Duktape * with certain config options, but allow user to * set them on a case-by-case basis. */ duk_get_prop_stridx_short(ctx, -2, DUK_STRIDX_NAME); duk_get_prop_stridx_short(ctx, -3, DUK_STRIDX_FILE_NAME); #if defined(DUK_USE_PC2LINE) line = duk_hobject_pc2line_query(ctx, -4, (duk_uint_fast32_t) pc); #else line = 0; #endif /* [ ... v1 v2 name filename ] */ /* When looking for .fileName/.lineNumber, blame first * function which has a .fileName. */ if (duk_is_string_notsymbol(ctx, -1)) { if (output_type == DUK__OUTPUT_TYPE_FILENAME) { return 1; } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) { duk_push_int(ctx, line); return 1; } } /* XXX: Change 'anon' handling here too, to use empty string for anonymous functions? */ /* XXX: Could be improved by coercing to a readable duk_tval (especially string escaping) */ h_name = duk_get_hstring_notsymbol(ctx, -2); /* may be NULL */ funcname = (h_name == NULL || h_name == DUK_HTHREAD_STRING_EMPTY_STRING(thr)) ? "[anon]" : (const char *) DUK_HSTRING_GET_DATA(h_name); filename = duk_get_string_notsymbol(ctx, -1); filename = filename ? filename : ""; DUK_ASSERT(funcname != NULL); DUK_ASSERT(filename != NULL); if (h_func == NULL) { duk_push_sprintf(ctx, "at %s light%s%s%s%s%s", (const char *) funcname, (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty)); } else if (DUK_HOBJECT_HAS_NATFUNC(h_func)) { duk_push_sprintf(ctx, "at %s (%s) native%s%s%s%s%s", (const char *) funcname, (const char *) filename, (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty)); } else { duk_push_sprintf(ctx, "at %s (%s:%ld)%s%s%s%s%s", (const char *) funcname, (const char *) filename, (long) line, (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty), (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty), (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty), (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty), (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty)); } duk_replace(ctx, -5); /* [ ... v1 v2 name filename str ] -> [ ... str v2 name filename ] */ duk_pop_3(ctx); /* -> [ ... str ] */ } else if (t == DUK_TYPE_STRING) { const char *str_file; /* * __FILE__ / __LINE__ entry, here 'pc' is line number directly. * Sometimes __FILE__ / __LINE__ is reported as the source for * the error (fileName, lineNumber), sometimes not. */ /* [ ... v1(filename) v2(line+flags) ] */ /* When looking for .fileName/.lineNumber, blame compilation * or C call site unless flagged not to do so. */ if (!(flags & DUK_TB_FLAG_NOBLAME_FILELINE)) { if (output_type == DUK__OUTPUT_TYPE_FILENAME) { duk_pop(ctx); return 1; } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) { duk_push_int(ctx, pc); return 1; } } /* Tracedata is trusted but avoid any risk of using a NULL * for %s format because it has undefined behavior. Symbols * don't need to be explicitly rejected as they pose no memory * safety issues. */ str_file = (const char *) duk_get_string(ctx, -2); duk_push_sprintf(ctx, "at [anon] (%s:%ld) internal", (const char *) (str_file ? str_file : "null"), (long) pc); duk_replace(ctx, -3); /* [ ... v1 v2 str ] -> [ ... str v2 ] */ duk_pop(ctx); /* -> [ ... str ] */ } else { /* unknown, ignore */ duk_pop_2(ctx); break; } } if (count_func >= DUK_USE_TRACEBACK_DEPTH) { /* Possibly truncated; there is no explicit truncation * marker so this is the best we can do. */ duk_push_hstring_stridx(ctx, DUK_STRIDX_BRACKETED_ELLIPSIS); } } /* [ ... this tracedata sep this str1 ... strN ] */ if (output_type != DUK__OUTPUT_TYPE_TRACEBACK) { return 0; } else { /* The 'this' after 'sep' will get ToString() coerced by * duk_join() automatically. We don't want to do that * coercion when providing .fileName or .lineNumber (GH-254). */ duk_join(ctx, duk_get_top(ctx) - (idx_td + 2) /*count, not including sep*/); return 1; } }
void StyleContext::parseStyleResult(StyleParamKey _key, StyleParam::Value& _val) const { _val = none_type{}; if (duk_is_string(m_ctx, -1)) { std::string value(duk_get_string(m_ctx, -1)); _val = StyleParam::parseString(_key, value); } else if (duk_is_boolean(m_ctx, -1)) { bool value = duk_get_boolean(m_ctx, -1); switch (_key) { case StyleParamKey::interactive: case StyleParamKey::text_interactive: case StyleParamKey::visible: _val = value; break; case StyleParamKey::extrude: _val = value ? glm::vec2(NAN, NAN) : glm::vec2(0.0f, 0.0f); break; default: break; } } else if (duk_is_array(m_ctx, -1)) { duk_get_prop_string(m_ctx, -1, "length"); int len = duk_get_int(m_ctx, -1); duk_pop(m_ctx); switch (_key) { case StyleParamKey::extrude: { if (len != 2) { LOGW("Wrong array size for extrusion: '%d'.", len); break; } duk_get_prop_index(m_ctx, -1, 0); double v1 = duk_get_number(m_ctx, -1); duk_pop(m_ctx); duk_get_prop_index(m_ctx, -1, 1); double v2 = duk_get_number(m_ctx, -1); duk_pop(m_ctx); _val = glm::vec2(v1, v2); break; } case StyleParamKey::color: case StyleParamKey::outline_color: case StyleParamKey::text_font_fill: case StyleParamKey::text_font_stroke_color: { if (len < 3 || len > 4) { LOGW("Wrong array size for color: '%d'.", len); break; } duk_get_prop_index(m_ctx, -1, 0); double r = duk_get_number(m_ctx, -1); duk_pop(m_ctx); duk_get_prop_index(m_ctx, -1, 1); double g = duk_get_number(m_ctx, -1); duk_pop(m_ctx); duk_get_prop_index(m_ctx, -1, 2); double b = duk_get_number(m_ctx, -1); duk_pop(m_ctx); double a = 1.0; if (len == 4) { duk_get_prop_index(m_ctx, -1, 3); a = duk_get_number(m_ctx, -1); duk_pop(m_ctx); } _val = (((uint32_t)(255.0 * a) & 0xff) << 24) | (((uint32_t)(255.0 * r) & 0xff)<< 16) | (((uint32_t)(255.0 * g) & 0xff)<< 8) | (((uint32_t)(255.0 * b) & 0xff)); break; } default: break; } } else if (duk_is_nan(m_ctx, -1)) { // Ignore setting value LOGD("duk evaluates JS method to NAN.\n"); } else if (duk_is_number(m_ctx, -1)) { switch (_key) { case StyleParamKey::extrude: _val = glm::vec2(0.f, static_cast<float>(duk_get_number(m_ctx, -1))); break; case StyleParamKey::width: case StyleParamKey::outline_width: { // TODO more efficient way to return pixels. // atm this only works by return value as string double v = duk_get_number(m_ctx, -1); _val = StyleParam::Width{static_cast<float>(v)}; break; } case StyleParamKey::text_font_stroke_width: { _val = static_cast<float>(duk_get_number(m_ctx, -1)); break; } case StyleParamKey::order: case StyleParamKey::outline_order: case StyleParamKey::priority: case StyleParamKey::color: case StyleParamKey::outline_color: case StyleParamKey::text_font_fill: case StyleParamKey::text_font_stroke_color: { _val = static_cast<uint32_t>(duk_get_uint(m_ctx, -1)); break; } default: break; } } else if (duk_is_null_or_undefined(m_ctx, -1)) { // Ignore setting value LOGD("duk evaluates JS method to null or undefined."); } else { LOGW("Unhandled return type from Javascript style function for %d.", _key); } DUMP("parseStyleResult\n"); }
/* basic enum success cases */ int test_1(duk_context *ctx) { duk_set_top(ctx, 0); printf("object with own properties only, enum with get_value=0\n"); duk_eval_string(ctx, "({ 1: 1, foo: 2, bar: 3, quux: 4, 2: 5 })"); duk_enum(ctx, -1, 0 /*enum_flags*/); /* [ obj enum ] */ while (duk_next(ctx, -1 /*enum_index*/, 0 /*get_value*/)) { printf("key: '%s'\n", duk_get_string(ctx, -1)); duk_pop(ctx); } duk_pop_2(ctx); printf("object with own properties only, enum with get_value=1\n"); duk_eval_string(ctx, "({ 1: 1, foo: 2, bar: 3, quux: 4, 2: 5 })"); duk_enum(ctx, -1, 0 /*enum_flags*/); /* [ obj enum ] */ while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) { printf("key: '%s', value: '%s'\n", duk_get_string(ctx, -2), duk_to_string(ctx, -1)); duk_pop_2(ctx); } duk_pop(ctx); duk_pop(ctx); printf("object with inherited, enumerable properties, enum with get_value=1\n"); duk_eval_string(ctx, "(function () { var o = Object.create({ parent: 'inherited' }); o.foo = 'bar'; return o; })()"); duk_enum(ctx, -1, 0 /*enum_flags*/); /* [ obj enum ] */ while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) { printf("key: '%s', value: '%s'\n", duk_get_string(ctx, -2), duk_to_string(ctx, -1)); duk_pop_2(ctx); } duk_pop(ctx); duk_pop(ctx); printf("object with own non-enumerable properties, enum with get_value=1, don't enum inherited properties\n"); duk_eval_string(ctx, "(function () { var o = Object.create({ parent: 'inherited' }); " "Object.defineProperty(o, 'enumerable_prop', { value: 123, writable: true, enumerable: true, configurable: true}); " "Object.defineProperty(o, 'nonenumerable_prop', { value: 234, writable: true, enumerable: false, configurable: true}); " " return o; })()"); printf("- enum only enumerable own properties\n"); duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY /*enum_flags*/); /* [ obj enum ] */ while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) { printf("key: '%s', value: '%s'\n", duk_get_string(ctx, -2), duk_to_string(ctx, -1)); duk_pop_2(ctx); } duk_pop(ctx); printf("- enum all own properties\n"); duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY | DUK_ENUM_INCLUDE_NONENUMERABLE /*enum_flags*/); /* [ obj enum ] */ while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) { printf("key: '%s', value: '%s'\n", duk_get_string(ctx, -2), duk_to_string(ctx, -1)); duk_pop_2(ctx); } duk_pop(ctx); duk_pop(ctx); printf("object with string and array index keys, enum with get_value=1\n"); duk_eval_string(ctx, "({ foo: 'val1', 999: 'val2', 1: 'val3', 123: 'val4', 234: 'val5', 2: 'val6', bar: 'val7' })"); printf("- enum array indices only, not sorted\n"); duk_enum(ctx, -1, DUK_ENUM_ARRAY_INDICES_ONLY /*enum_flags*/); /* [ obj enum ] */ while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) { printf("key: '%s', value: '%s'\n", duk_get_string(ctx, -2), duk_to_string(ctx, -1)); duk_pop_2(ctx); } duk_pop(ctx); printf("- enum array indices only, sorted\n"); duk_enum(ctx, -1, DUK_ENUM_ARRAY_INDICES_ONLY | DUK_ENUM_SORT_ARRAY_INDICES /*enum_flags*/); /* [ obj enum ] */ while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) { printf("key: '%s', value: '%s'\n", duk_get_string(ctx, -2), duk_to_string(ctx, -1)); duk_pop_2(ctx); } duk_pop(ctx); duk_pop(ctx); /* XXX: there is no test for DUK_ENUM_INCLUDE_INTERNAL now, * this is a bit difficult because internal properties are * not exposed or stable. */ printf("final top: %d\n", duk_get_top(ctx)); return 0; }
int8_t sendSms(duk_context *JsContext, InputParameters_t *Parameters, char *Input[]) { extern const char const *ErrorMessages[]; extern HttpHandle_t *Handle; extern Output_t OutputMessages; int8_t RetCode = EXIT_SUCCESS; char *GatewayLocation; if (Parameters->GatewayLocation) { uint8_t GatewayLocationLength = strlen(Input[Parameters->GatewayLocation] ); if ( Input[Parameters->GatewayLocation][GatewayLocationLength - 1] == '/') { Input[Parameters->GatewayLocation][GatewayLocationLength - 1] = '\0'; GatewayLocationLength--; } // 10 = '/' + ".gateway" + '\0' GatewayLocation = malloc(GatewayLocationLength + strlen(Input[Parameters->Gateway] ) + 10); if (!GatewayLocation) { setErrorMsg(ErrorMessages[MEMORY], true); return NO_MEMORY; } sprintf(GatewayLocation, "%s/%s.gateway",Input[Parameters->GatewayLocation], Input[Parameters->Gateway] ); } else { // 27 = "/usr/share/esmska/" + ".gateway" + '\0' GatewayLocation = malloc(strlen(Input[Parameters->Gateway] ) + 27); if (!GatewayLocation) { setErrorMsg(ErrorMessages[MEMORY], true); return NO_MEMORY; } sprintf(GatewayLocation, "/usr/share/esmska/%s.gateway", Input[Parameters->Gateway] ); } if (duk_peval_file(JsContext, GatewayLocation) ) { duk_get_prop_string(JsContext, -1, "message"); setErrorMsg(ErrorMessages[MODULE_PROCESSING], false); setErrorMsg(Input[Parameters->Gateway], false); setErrorMsg(duk_get_string(JsContext, -1), true); free(GatewayLocation); return MODULE_ERROR; } free(GatewayLocation); duk_pop(JsContext); // Zahozeni vysledku (undefined) // Zpristupneni potrebnych udaju jako username, passwd, cislo, text zpravy duk_push_global_object(JsContext); if (Parameters->User) duk_push_string(JsContext, Input[Parameters->User] ); else duk_push_string(JsContext, ""); duk_put_prop_string(JsContext, -2, "LOGIN"); if (Parameters->Password) duk_push_string(JsContext, Input[Parameters->Password] ); else duk_push_string(JsContext, ""); duk_put_prop_string(JsContext, -2, "PASSWORD"); if (Parameters->SenderNumber) duk_push_string(JsContext, Input[Parameters->SenderNumber] ); else duk_push_string(JsContext, ""); duk_put_prop_string(JsContext, -2, "SENDERNUMBER"); duk_push_string(JsContext, Input[Parameters->Number] ); duk_put_prop_string(JsContext, -2, "NUMBER"); duk_push_string(JsContext, Input[Parameters->Text] ); duk_put_prop_string(JsContext, -2, "MESSAGE"); if (duk_peval_string(JsContext, "RECEIPT = (getFeatures().indexOf('RECEIPT') > -1 ? true : false)") ) { duk_get_prop_string(JsContext, -1, "message"); setErrorMsg(ErrorMessages[RECEIPT_SETTING], false); setErrorMsg(duk_get_string(JsContext, -1), true); return MODULE_ERROR; } duk_pop(JsContext); // Zpristupneni potrebnych metod, promennych a samotny objekt EXEC duk_push_object(JsContext); duk_push_c_function(JsContext, get, 2); duk_put_prop_string(JsContext, -2, "getURL"); duk_push_c_function(JsContext, post, 3); duk_put_prop_string(JsContext, -2, "postURL"); duk_push_c_function(JsContext, setProblem, 2); duk_put_prop_string(JsContext, -2, "setProblem"); duk_push_c_function(JsContext, setSupplementalMsg, 1); duk_put_prop_string(JsContext, -2, "setSupplementalMessage"); duk_push_c_function(JsContext, stall, 1); duk_put_prop_string(JsContext, -2, "sleep"); duk_push_c_function(JsContext, setReferer, 1); duk_put_prop_string(JsContext, -2, "setReferer"); duk_push_string(JsContext, "Free SMS remaining: "); duk_put_prop_string(JsContext, -2, "INFO_FREE_SMS_REMAINING"); duk_push_string(JsContext, "The gateway doesn't provide any information about successful sending. The message might be and might not be delivered."); duk_put_prop_string(JsContext, -2, "INFO_STATUS_NOT_PROVIDED"); duk_push_string(JsContext, "Remaining credit: "); duk_put_prop_string(JsContext, -2, "INFO_CREDIT_REMAINING"); duk_put_prop_string(JsContext, -2, "EXEC"); // Vytvoreni CURL contextu if ( !(Handle = httpInit() ) ) { setErrorMsg(ErrorMessages[NO_CURL_CONTEXT], true); return CURL_ERROR; } // Volani funkce send() duk_get_prop_string(JsContext, -1, "send"); if (duk_pcall(JsContext, 0) ) { if (duk_is_number(JsContext, -1) ) { uint8_t ErrCode = duk_get_number(JsContext, -1); if (ErrCode < 90) { setErrorMsg(ErrorMessages[GENERIC], false); setErrorMsg(curl_easy_strerror(ErrCode), true); RetCode = CURL_ERROR; } else { OutputMessages.MessagesArray[OutputMessages.NextFree].MsgType = ERR; OutputMessages.MessagesArray[OutputMessages.NextFree].IsEndOfMsg = 1; switch (ErrCode) { case NO_MEMORY: OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[MEMORY]; RetCode = INTERNAL_ERROR; break; case NO_POST_DATA: OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[POST_DATA]; RetCode = MODULE_ERROR; break; case SET_PROBLEM_NO_MSG: OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[NO_EXPLAINING]; RetCode = MODULE_ERROR; break; case SLEEP_NOT_NUMBER: OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[SLEEP_NUMBER]; RetCode = MODULE_ERROR; break; case ODD_URL_PARAMETERS: OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[URL_PARAMETERS]; RetCode = MODULE_ERROR; break; case ODD_POST_PARAMETERS: OutputMessages.MessagesArray[OutputMessages.NextFree++].Msg = ErrorMessages[POST_PARAMETERS]; RetCode = MODULE_ERROR; break; } } } else { setErrorMsg(duk_safe_to_string(JsContext, -1), true); RetCode = DUKTAPE_ERROR; } } else { bool SendRetVal; if (duk_is_boolean(JsContext, -1) ) SendRetVal = duk_get_boolean(JsContext, -1); else { setErrorMsg(ErrorMessages[NOT_BOOL], true); SendRetVal = (bool) ( (int8_t) duk_to_number(JsContext, -1) ); } if (!SendRetVal) RetCode = GATEWAY_ERROR; } httpCleanUp(Handle); return RetCode; }
/* * Called with "sessions" at the top of the stack. * * Iterate over the accumulated announcents and make callbacks into JavaScript */ static void AnnouncementCallbacks(duk_context* ctx, const char* peer, SessionInfo* sessionInfo) { size_t i; size_t numSvcs; AJ_ASSERT(sessionInfo->sessionId); /* * Get the session object (indexed by the peer string) and from this get the announcements array */ duk_get_prop_string(ctx, -1, peer); duk_get_prop_string(ctx, -1, "anno"); /* * Iterate over the services implemented by this peer */ numSvcs = duk_get_length(ctx, -1); for (i = 0; i < numSvcs; ++i) { size_t j; size_t numIfaces; duk_idx_t svcIdx; duk_get_prop_index(ctx, -1, i); if (duk_is_undefined(ctx, -1)) { /* * Undefined means the iface has been deleted so pop "interfaces" */ duk_pop(ctx); /* * Delete the announcement entry and continue */ duk_del_prop_index(ctx, -1, i); continue; } svcIdx = duk_get_top_index(ctx); /* * Iterate over the interfaces for this service */ duk_get_prop_string(ctx, svcIdx, "interfaces"); numIfaces = duk_get_length(ctx, -1); for (j = 0; j < numIfaces; ++j) { const char* iface; duk_get_prop_index(ctx, -1, j); iface = duk_get_string(ctx, -1); duk_pop(ctx); /* * If there is a callback registered for this interface call it * * TODO - should we really make a callback for each interface or just call the first one * that has an callback referenced. If the same callback function has been registered * for multiple interfaces we definitely don't want to call it multiple times. */ if (PushServiceCallback(ctx, iface)) { /* * Set the session information on the service */ duk_push_int(ctx, sessionInfo->sessionId); duk_put_prop_string(ctx, svcIdx, "session"); duk_push_string(ctx, peer); duk_put_prop_string(ctx, svcIdx, "dest"); /* * Call the callback function */ duk_dup(ctx, svcIdx); if (duk_pcall(ctx, 1) != DUK_EXEC_SUCCESS) { AJS_ConsoleSignalError(ctx); } duk_pop(ctx); } } /* * Pop "interfaces" and the service object */ duk_pop_2(ctx); /* * Delete the announcement entry */ duk_del_prop_index(ctx, -1, i); } /* Pop "anno" and the session object */ duk_pop_2(ctx); }