void _gum_duk_push_native_pointer (duk_context * ctx, gpointer address, GumDukCore * core) { GumDukNativePointerImpl * ptr; ptr = core->cached_native_pointers; if (ptr != NULL) { core->cached_native_pointers = ptr->next; duk_push_heapptr (ctx, ptr->object); ptr->parent.value = address; duk_push_global_stash (ctx); duk_del_prop_string (ctx, -1, ptr->id); duk_pop (ctx); return; } duk_push_heapptr (ctx, core->native_pointer); duk_push_pointer (ctx, address); duk_new (ctx, 1); }
static void duk__push_module_object(duk_context *ctx, const char *id, duk_bool_t main) { duk_push_object(ctx); /* Set this as the main module, if requested */ if (main) { duk_push_global_stash(ctx); duk_dup(ctx, -2); duk_put_prop_string(ctx, -2, "\xff" "mainModule"); duk_pop(ctx); } /* Node.js uses the canonicalized filename of a module for both module.id * and module.filename. We have no concept of a file system here, so just * use the module ID for both values. */ duk_push_string(ctx, id); duk_dup(ctx, -1); duk_put_prop_string(ctx, -3, "filename"); duk_put_prop_string(ctx, -2, "id"); /* module.exports = {} */ duk_push_object(ctx); duk_put_prop_string(ctx, -2, "exports"); /* module.loaded = false */ duk_push_false(ctx); duk_put_prop_string(ctx, -2, "loaded"); /* module.require */ duk__push_require_function(ctx, id); duk_put_prop_string(ctx, -2, "require"); }
Variant DuktapeScriptFunction::call(const std::vector<Variant> & args) { // Get function wrapper from stash duk_push_global_stash(m_context); duk_get_prop_index(m_context, -1, m_stashIndex); // Push arguments for (Variant var : args) { m_scriptBackend->pushToDukStack(var); } // Call function duk_int_t error = duk_pcall(m_context, args.size()); // Check for error if (error) { // Raise script exception m_scriptBackend->m_scriptContext->scriptException(std::string(duk_safe_to_string(m_context, -1))); duk_pop_2(m_context); return Variant(); } // Convert return value Variant value = m_scriptBackend->fromDukStack(-1); duk_pop_2(m_context); return value; }
void JSEventDispatcher::EndSendEvent(Context* context, Object* sender, StringHash eventType, VariantMap& eventData) { if (!jsEvents_.Contains(eventType)) return; JSVM* vm = JSVM::GetJSVM(NULL); if (!vm) return; duk_context* ctx = vm->GetJSContext(); duk_push_global_stash(ctx); duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_VARIANTMAP_CACHE); duk_push_pointer(ctx, (void*) &eventData); duk_get_prop(ctx, -2); // If this issue is addressed, revisit this to simply remove // the variantmap object from the cache, if the user explicitly // keeps the event object alive in a local closure, that is allowed // (though, will keep object properties from being GC'd) // https://github.com/svaarala/duktape/issues/229 // Ok, this is unfortunate, in an event callback it is possible // to capture the Proxy object which represents the event VariantMap // in a function() {} closure, which will keep the event data alive // until the function happens to be gc'd (it is a member of the eventhandler) // which will leave things like scenes up if there was a P_SCENE event data // member, etc // So, we need to check if we have an object in the variant map cache // and thense call it's delete property method on the Proxy, which will clear // all the data (the proxy can still be alive as a captured local, though // the members won't be held // This all makes it that much more important that the pointer to eventData // is consistent across entire event, otherwise references may be held // see note above about: https://github.com/svaarala/duktape/issues/229 if (duk_is_object(ctx, -1)) { // deletes all properties, thus freeing references, even if // the variant map object is held onto by script (it will be invalid, post // event send) // see JSAPI.cpp variantmap_property_deleteproperty duk_del_prop_index(ctx, -1, 0); duk_push_pointer(ctx, (void*) &eventData); duk_push_undefined(ctx); // clear the variant map object from the cache duk_put_prop(ctx, -4); } duk_pop_3(ctx); }
void test(duk_context *ctx) { printf("top: %d\n", duk_get_top(ctx)); duk_push_global_stash(ctx); printf("top: %d\n", duk_get_top(ctx)); duk_push_int(ctx, 123); duk_put_prop_string(ctx, -2, "myvalue"); duk_pop(ctx); printf("top: %d\n", duk_get_top(ctx)); duk_push_global_stash(ctx); duk_get_prop_string(ctx, -1, "myvalue"); printf("value: %d\n", duk_get_int(ctx, -1)); duk_pop(ctx); duk_pop(ctx); printf("top: %d\n", duk_get_top(ctx)); }
void duk_module_node_init(duk_context *ctx) { /* * Stack: [ ... options ] => [ ... ] */ duk_idx_t options_idx; duk_require_object_coercible(ctx, -1); /* error before setting up requireCache */ options_idx = duk_require_normalize_index(ctx, -1); /* Initialize the require cache to a fresh object. */ duk_push_global_stash(ctx); duk_push_bare_object(ctx); duk_put_prop_string(ctx, -2, "\xff" "requireCache"); duk_pop(ctx); /* Stash callbacks for later use. User code can overwrite them later * on directly by accessing the global stash. */ duk_push_global_stash(ctx); duk_get_prop_string(ctx, options_idx, "resolve"); duk_require_function(ctx, -1); duk_put_prop_string(ctx, -2, "\xff" "modResolve"); duk_get_prop_string(ctx, options_idx, "load"); duk_require_function(ctx, -1); duk_put_prop_string(ctx, -2, "\xff" "modLoad"); duk_pop(ctx); /* Stash main module. */ duk_push_global_stash(ctx); duk_push_undefined(ctx); duk_put_prop_string(ctx, -2, "\xff" "mainModule"); duk_pop(ctx); /* register `require` as a global function. */ duk_push_global_object(ctx); duk_push_string(ctx, "require"); duk__push_require_function(ctx, ""); duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_SET_WRITABLE | DUK_DEFPROP_SET_CONFIGURABLE); duk_pop(ctx); duk_pop(ctx); /* pop argument */ }
static tic_machine* getDukMachine(duk_context* duk) { duk_push_global_stash(duk); duk_get_prop_string(duk, -1, TicMachine); tic_machine* machine = duk_to_pointer(duk, -1); duk_pop_2(duk); return machine; }
static int init_binding_worker(duk_context *ctx) { duk_push_global_stash(ctx); duk_push_object(ctx); duk_put_prop_string(ctx, -2, "comoWorkersCallBack"); duk_pop(ctx); duk_push_object(ctx); duk_put_function_list(ctx, -1, como_worker_funcs); return 1; }
static void duk__push_require_function(duk_context *ctx, const char *id) { duk_push_c_function(ctx, duk__handle_require, 1); duk_push_string(ctx, "name"); duk_push_string(ctx, "require"); duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE); duk_push_string(ctx, id); duk_put_prop_string(ctx, -2, "\xff" "moduleId"); /* require.cache */ duk_push_global_stash(ctx); (void) duk_get_prop_string(ctx, -1, "\xff" "requireCache"); duk_put_prop_string(ctx, -3, "cache"); duk_pop(ctx); /* require.main */ duk_push_global_stash(ctx); (void) duk_get_prop_string(ctx, -1, "\xff" "mainModule"); duk_put_prop_string(ctx, -3, "main"); duk_pop(ctx); }
static duk_bool_t duk__get_cached_module(duk_context *ctx, const char *id) { duk_push_global_stash(ctx); (void) duk_get_prop_string(ctx, -1, "\xff" "requireCache"); if (duk_get_prop_string(ctx, -1, id)) { duk_remove(ctx, -2); duk_remove(ctx, -2); return 1; } else { duk_pop_3(ctx); return 0; } }
DuktapeContext::DuktapeContext(JavaVM* javaVM) : m_context(duk_create_heap(nullptr, nullptr, nullptr, nullptr, fatalErrorHandler)) , m_objectType(m_javaValues.getObjectType(getEnvFromJavaVM(javaVM))) { if (!m_context) { throw std::bad_alloc(); } // Stash the JVM object in the context, so we can find our way back from a Duktape C callback. duk_push_global_stash(m_context); duk_push_pointer(m_context, javaVM); duk_put_prop_string(m_context, -2, JAVA_VM_PROP_NAME); duk_pop(m_context); }
static BindingInfo * GetBinding( duk_context * ctx ) { duk_push_global_stash( ctx ); duk_get_prop_string( ctx, -1, DUKBIND_BINDING_NAME ); duk_size_t buffer_size; void * buffer = duk_to_buffer( ctx, -1, &buffer_size); dukbind_assert( buffer_size == sizeof( BindingInfo * ), "Invalid buffer size" ); duk_pop_2( ctx ); return *reinterpret_cast<BindingInfo**>( buffer ); }
static int _worker_dispatch_cb (evHandle *handle){ comoWorker *worker = handle->data; duk_context *ctx = worker->Mainctx; mtx_lock(&worker->mtx); QUEUE *q; while ( !QUEUE_EMPTY(&worker->queueOut) ){ q = QUEUE_HEAD(&(worker)->queueOut); QUEUE_REMOVE(q); comoQueue *queue = QUEUE_DATA(q, comoQueue, queue); if (worker->destroy != 0){ goto FREE; } duk_push_heapptr(ctx, worker->self); if (duk_get_type(ctx, -1) != DUK_TYPE_OBJECT){ dump_stack(ctx, "DUK"); assert(0); } como_push_worker_value(ctx, queue); duk_call(ctx, 1); duk_pop(ctx); FREE : /* free except in case of pointers */ if (queue->data != NULL && queue->type != DUK_TYPE_POINTER){ free(queue->data); } free(queue); } mtx_unlock(&worker->mtx); if (worker->destroy == 2){ duk_push_global_stash(ctx); duk_get_prop_string(ctx, -1, "comoWorkersCallBack"); duk_push_number(ctx, (double) handle->id); duk_del_prop(ctx, -2); handle_close(handle); free(worker); } return 0; }
/* Place a `module` object on the top of the value stack into the require cache * based on its `.id` property. As a convenience to the caller, leave the * object on top of the value stack afterwards. */ static void duk__put_cached_module(duk_context *ctx) { /* [ ... module ] */ duk_push_global_stash(ctx); (void) duk_get_prop_string(ctx, -1, "\xff" "requireCache"); duk_dup(ctx, -3); /* [ ... module stash req_cache module ] */ (void) duk_get_prop_string(ctx, -1, "id"); duk_dup(ctx, -2); duk_put_prop(ctx, -4); duk_pop_3(ctx); /* [ ... module ] */ }
void duk_module_duktape_init(duk_context *ctx) { /* Stash 'Duktape' in case it's modified. */ duk_push_global_stash(ctx); duk_get_global_string(ctx, "Duktape"); duk_put_prop_string(ctx, -2, "\xff" "module:Duktape"); duk_pop(ctx); /* Register `require` as a global function. */ duk_eval_string(ctx, "(function(req){" "var D=Object.defineProperty;" "D(req,'name',{value:'require'});" "D(this,'require',{value:req,writable:true,configurable:true});" "D(Duktape,'modLoaded',{value:Object.create(null),writable:true,configurable:true});" "})"); duk_push_c_function(ctx, duk__require, 1 /*nargs*/); duk_call(ctx, 1); duk_pop(ctx); }
void GCObjectPool::removeList() { Isolate *isolate = Isolate::GetCurrent(); duk_context *ctx = isolate->GetDukContext(); // Get the "refs" array in the heap stash duk_push_global_stash(ctx); duk_get_prop_string(ctx, -1, "__object_list"); duk_remove(ctx, -2); // scopeList[scopeIndex] = scopeList[0] duk_get_prop_index(ctx, -1, 0); // <scopeList> <scopeList[0]> duk_put_prop_index(ctx, -2, (duk_uarridx_t) list_idx_); // scopeList[0] = scopeIndex duk_push_int(ctx, list_idx_); // <scopeList> <scopeIndex> duk_put_prop_index(ctx, -2, 0); // <scopeList> duk_pop(ctx); // }
static int duk_disasm(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { int res = 0, res2 = 0; const char *opstr = NULL; ut8 *b = a->cur->user; duk_push_global_stash (ctx); duk_dup (ctx, 0); /* timer callback */ duk_get_prop_string (ctx, -2, "disfun"); b = a->cur->user = duk_require_tval (ctx, -1); // pushBuffer (buf, len); if (duk_is_callable(ctx, -1)) { int i; // duk_push_string (ctx, "TODO 2"); pushBuffer (buf, len); duk_call (ctx, 1); // [ size, str ] for (i = 0; i<3; i++) { duk_dup_top (ctx); duk_get_prop_index (ctx, -1, i); if (duk_is_number (ctx, -1)) { if (res) res2 = duk_to_number (ctx, -1); else res2 = res = duk_to_number (ctx, -1); } else if (duk_is_string (ctx, -1)) { if (!opstr) { opstr = duk_to_string (ctx, -1); } } duk_pop (ctx); } } else { eprintf ("[:(] Is not a function %02x %02x\n", b[0],b[1]); } // fill op struct op->size = res; if (!opstr) opstr = "invalid"; strncpy (op->buf_asm, opstr, sizeof (op->buf_asm)); r_hex_bin2str (buf, op->size, op->buf_hex); return res2; }
void _gum_duk_protect (duk_context * ctx, GumDukHeapPtr object) { gchar name[32]; duk_uint_t ref_count; if (object == NULL) return; sprintf (name, "protected_%p", object); duk_push_global_stash (ctx); duk_get_prop_string (ctx, -1, name); if (duk_is_undefined (ctx, -1)) { duk_pop (ctx); duk_push_object (ctx); duk_push_heapptr (ctx, object); duk_put_prop_string (ctx, -2, "o"); ref_count = 1; duk_push_uint (ctx, ref_count); duk_put_prop_string (ctx, -2, "n"); duk_put_prop_string (ctx, -2, name); } else { duk_get_prop_string (ctx, -1, "n"); ref_count = duk_get_uint (ctx, -1); duk_pop (ctx); ref_count++; duk_push_uint (ctx, ref_count); duk_put_prop_string (ctx, -2, "n"); duk_pop (ctx); } duk_pop (ctx); }
static void initDuktape(tic_machine* machine) { closeJavascript((tic_mem*)machine); duk_context* duk = machine->js = duk_create_heap(NULL, NULL, NULL, machine, NULL); { duk_push_global_stash(duk); duk_push_pointer(duk, machine); duk_put_prop_string(duk, -2, TicMachine); duk_pop(duk); } for (s32 i = 0; i < COUNT_OF(ApiFunc); i++) if (ApiFunc[i].func) { duk_push_c_function(machine->js, ApiFunc[i].func, ApiFunc[i].params); duk_put_global_string(machine->js, ApiKeywords[i]); } }
int UI_DebugGetUIKeepAliveCount(duk_context* ctx) { duk_push_global_stash(ctx); duk_get_prop_string(ctx, -1, "__jsui_widgetkeepalive"); duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY); double count = 0; while (duk_next(ctx, -1 , 0)) { duk_pop(ctx); /* pop_key */ count++; } duk_pop_n(ctx, 3); /* pop enum object, keep alive object, and stash */ duk_push_number(ctx, count); return 1; }
void GlobalStash::RemoveObject(int index) { if (!index) return; Isolate *isolate = Isolate::GetCurrent(); duk_context *ctx = isolate->GetDukContext(); // Get the "refs" array in the heap stash duk_push_global_stash(ctx); duk_get_prop_string(ctx, -1, name_); duk_remove(ctx, -2); // scopeList[scopeIndex] = scopeList[0] duk_get_prop_index(ctx, -1, 0); // <scopeList> <scopeList[0]> duk_put_prop_index(ctx, -2, (duk_uarridx_t) index); // scopeList[0] = scopeIndex duk_push_int(ctx, index); // <scopeList> <scopeIndex> duk_put_prop_index(ctx, -2, 0); // <scopeList> duk_pop(ctx); // }
void GCObjectPool::addNewList() { Isolate *isolate = Isolate::GetCurrent(); duk_context *ctx = isolate->GetDukContext(); // Get the "refs" array in the heap stash duk_push_global_stash(ctx); duk_get_prop_string(ctx, -1, "__object_list"); duk_remove(ctx, -2); int type = duk_get_type(ctx, -1); int freeSlot; // freeSlot = scopeList[0] duk_get_prop_index(ctx, -1, 0); // <scopeList> <scopeList[0]> freeSlot = duk_get_int(ctx, -1); duk_pop(ctx); // <scopeList> if (freeSlot != 0) { // scopeList[0] = scopeList[freeSlot] duk_get_prop_index(ctx, -1, (duk_uarridx_t) freeSlot); // <scopeList> <scopeList[freeSlot]> duk_put_prop_index(ctx, -2, 0); // <scopeList> } else { // freeSlot = scopeList.length; freeSlot = (int) duk_get_length(ctx, -1); } duk_push_array(ctx); list_ptr_ = duk_get_heapptr(ctx, -1); // <scopeList> <scope> // scopeList[freeSlot] = scope duk_put_prop_index(ctx, -2, (duk_uarridx_t) freeSlot); // <scopeList> // Remove the refs array from the stack. duk_pop(ctx); // list_idx_ = freeSlot; }
int GlobalStash::AddObject(void *ptr) { Isolate *isolate = Isolate::GetCurrent(); duk_context *ctx = isolate->GetDukContext(); // Get the "refs" array in the heap stash duk_push_global_stash(ctx); duk_get_prop_string(ctx, -1, name_); duk_remove(ctx, -2); int type = duk_get_type(ctx, -1); int freeSlot; // freeSlot = scopeList[0] duk_get_prop_index(ctx, -1, 0); // <scopeList> <scopeList[0]> freeSlot = duk_get_int(ctx, -1); duk_pop(ctx); // <scopeList> if (freeSlot != 0) { // scopeList[0] = scopeList[freeSlot] duk_get_prop_index(ctx, -1, (duk_uarridx_t) freeSlot); // <scopeList> <scopeList[freeSlot]> duk_put_prop_index(ctx, -2, 0); // <scopeList> } else { // freeSlot = scopeList.length; freeSlot = (int) duk_get_length(ctx, -1); } duk_push_heapptr(ctx, ptr); // <scopeList> <scope> // scopeList[freeSlot] = scope duk_put_prop_index(ctx, -2, (duk_uarridx_t) freeSlot); // <scopeList> // Remove the refs array from the stack. duk_pop(ctx); // return freeSlot; }
GlobalStash::GlobalStash(const char *name) : name_(name) { Isolate *isolate = Isolate::GetCurrent(); duk_context *ctx = isolate->GetDukContext(); // 每个 global 对象对应一个 stash 对象 duk_push_global_stash(ctx); duk_get_prop_string(ctx, -1, name_); if (duk_is_undefined(ctx, -1)) { duk_pop(ctx); // Create a new array with one `0` at index `0`. duk_push_array(ctx); duk_push_int(ctx, 0); duk_put_prop_index(ctx, -2, 0); // Store it as "name_" in the heap stash duk_put_prop_string(ctx, -2, name_); } else { duk_pop(ctx); } duk_pop(ctx); }
static int duk_assemble(RAsm *a, RAsmOp *op, const char *str) { int i, res = 0; // call myasm function if available duk_push_global_stash (ctx); duk_dup (ctx, 0); /* timer callback */ duk_get_prop_string (ctx, -2, "asmfun"); a->cur->user = duk_require_tval (ctx, -1); if (duk_is_callable(ctx, -1)) { duk_push_string (ctx, str); duk_call (ctx, 1); // [ array of bytes ] //duk_dup_top (ctx); res = duk_get_length (ctx, -1); op->size = res; for (i=0; i<res; i++) { duk_dup_top (ctx); duk_get_prop_index (ctx, -2, i); op->buf[i] = duk_to_int (ctx, -1); } } if (res<1) res = -1; return res; }
void GCObjectPool::EnsureObjectList() { Isolate *isolate = Isolate::GetCurrent(); duk_context *ctx = isolate->GetDukContext(); // 每个 global 对象对应一个 stash 对象 duk_push_global_stash(ctx); duk_get_prop_string(ctx, -1, "__object_list"); if (duk_is_undefined(ctx, -1)) { duk_pop(ctx); // Create a new array with one `0` at index `0`. duk_push_array(ctx); duk_push_int(ctx, 0); duk_put_prop_index(ctx, -2, 0); duk_dup_top(ctx); // Store it as "name_" in the heap stash duk_put_prop_string(ctx, -3, "__object_list"); } duk_pop_2(ctx); }
void AJS_RegisterMsgFunctions(AJ_BusAttachment* bus, duk_context* ctx, duk_idx_t ajIdx) { duk_idx_t objIdx; duk_push_c_function(ctx, NativeBroadcastSignal, 2); duk_put_prop_string(ctx, ajIdx, "signal"); duk_push_global_stash(ctx); /* * Push and initialized the peerProto object */ objIdx = duk_push_object(ctx); duk_put_number_list(ctx, objIdx, peer_native_numbers); AJS_PutFunctionList(ctx, objIdx, peer_native_functions, TRUE); /* * Finalizer function called when the object is deleted */ AJS_RegisterFinalizer(ctx, objIdx, NativeServiceObjectFinalizer); /* * Give the object a name */ duk_put_prop_string(ctx, -2, "peerProto"); duk_pop(ctx); }
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 test_basic(duk_context *ctx) { duk_idx_t i, n; void *ptr; void *p1, *p2, *p3; duk_int_t ret; duk_push_undefined(ctx); duk_push_null(ctx); duk_push_boolean(ctx, 1); duk_push_number(ctx, 123.0); duk_push_string(ctx, "test string"); duk_eval_string(ctx, "({ foo: 'bar' })"); duk_eval_string(ctx, "Duktape.dec('hex', 'deadbeef');"); /* buffer */ /* Print pointer NULL/non-NULL */ n = duk_get_top(ctx); for (i = 0; i < n + 1; i++) { /* Note: access i == n on purpose (invalid index) */ ptr = duk_get_heapptr(ctx, i); printf("top: %ld\n", (long) duk_get_top(ctx)); printf("idx %ld: type %ld, duk_get_heapptr() -> %s\n", (long) i, (long) duk_get_type(ctx, i), (ptr ? "non-NULL" : "NULL")); duk_push_uint(ctx, (duk_uint_t) i); ret = duk_safe_call(ctx, raw_require_heapptr, 1 /*nargs*/, 1 /*nrets*/); if (ret == DUK_EXEC_SUCCESS) { ; } else { printf("idx %ld: type %ld, duk_require_heapptr() -> %s\n", (long) i, (long) duk_get_type(ctx, i), duk_safe_to_string(ctx, -1)); } duk_pop(ctx); } /* Write the values to the global stash to simulate whatever * reachability mechanisms user code uses. */ n = duk_get_top(ctx); duk_push_global_stash(ctx); for (i = 0; i < n; i++) { duk_dup(ctx, i); duk_put_prop_index(ctx, -2, (duk_uarridx_t) i); } /* Get borrowed references */ p1 = duk_get_heapptr(ctx, 4); p2 = duk_get_heapptr(ctx, 5); p3 = duk_get_heapptr(ctx, 6); /* Erase value stack, simulating user code moving on and relying on * stashed values keeping the target values reachable. Force a GC * for good measure. */ duk_set_top(ctx, 0); duk_gc(ctx, 0); /* Push the values back one by one and test that they're intact. */ duk_eval_string(ctx, "(function (v) { print(Duktape.enc('jx', v)); })"); duk_dup_top(ctx); duk_push_heapptr(ctx, p1); duk_call(ctx, 1); duk_pop(ctx); duk_dup_top(ctx); duk_push_heapptr(ctx, p2); duk_call(ctx, 1); duk_pop(ctx); duk_dup_top(ctx); duk_push_heapptr(ctx, p3); duk_call(ctx, 1); duk_pop(ctx); duk_dup_top(ctx); duk_push_heapptr(ctx, NULL); duk_call(ctx, 1); duk_pop(ctx); duk_pop(ctx); /* Done. */ printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t duk__handle_require(duk_context *ctx) { /* * Value stack handling here is a bit sloppy but should be correct. * Call handling will clean up any extra garbage for us. */ const char *id; const char *parent_id; duk_idx_t module_idx; duk_idx_t stash_idx; duk_int_t ret; duk_push_global_stash(ctx); stash_idx = duk_normalize_index(ctx, -1); duk_push_current_function(ctx); (void) duk_get_prop_string(ctx, -1, "\xff" "moduleId"); parent_id = duk_require_string(ctx, -1); (void) parent_id; /* not used directly; suppress warning */ /* [ id stash require parent_id ] */ id = duk_require_string(ctx, 0); (void) duk_get_prop_string(ctx, stash_idx, "\xff" "modResolve"); duk_dup(ctx, 0); /* module ID */ duk_dup(ctx, -3); /* parent ID */ duk_call(ctx, 2); /* [ ... stash ... resolved_id ] */ id = duk_require_string(ctx, -1); if (duk__get_cached_module(ctx, id)) { goto have_module; /* use the cached module */ } duk__push_module_object(ctx, id, 0 /*main*/); duk__put_cached_module(ctx); /* module remains on stack */ /* * From here on out, we have to be careful not to throw. If it can't be * avoided, the error must be caught and the module removed from the * require cache before rethrowing. This allows the application to * reattempt loading the module. */ module_idx = duk_normalize_index(ctx, -1); /* [ ... stash ... resolved_id module ] */ (void) duk_get_prop_string(ctx, stash_idx, "\xff" "modLoad"); duk_dup(ctx, -3); /* resolved ID */ (void) duk_get_prop_string(ctx, module_idx, "exports"); duk_dup(ctx, module_idx); ret = duk_pcall(ctx, 3); if (ret != DUK_EXEC_SUCCESS) { duk__del_cached_module(ctx, id); duk_throw(ctx); /* rethrow */ } if (duk_is_string(ctx, -1)) { duk_int_t ret; /* [ ... module source ] */ #if DUK_VERSION >= 19999 ret = duk_safe_call(ctx, duk__eval_module_source, NULL, 2, 1); #else ret = duk_safe_call(ctx, duk__eval_module_source, 2, 1); #endif if (ret != DUK_EXEC_SUCCESS) { duk__del_cached_module(ctx, id); duk_throw(ctx); /* rethrow */ } } else if (duk_is_undefined(ctx, -1)) { duk_pop(ctx); } else { duk__del_cached_module(ctx, id); duk_error(ctx, DUK_ERR_TYPE_ERROR, "invalid module load callback return value"); } /* fall through */ have_module: /* [ ... module ] */ (void) duk_get_prop_string(ctx, -1, "exports"); return 1; }