/* duk_del_prop_string(), success cases */ static duk_ret_t test_delpropstring_a_safecall(duk_context *ctx, void *udata) { duk_ret_t rc; (void) udata; prep(ctx); rc = duk_del_prop_string(ctx, 0, "foo"); printf("delete obj.foo -> rc=%d\n", (int) rc); rc = duk_del_prop_string(ctx, 0, "nonexistent"); printf("delete obj.nonexistent -> rc=%d\n", (int) rc); rc = duk_del_prop_string(ctx, 0, "123"); printf("delete obj['123'] -> rc=%d\n", (int) rc); rc = duk_del_prop_string(ctx, 1, "nonexistent"); printf("delete arr.nonexistent -> rc=%d\n", (int) rc); rc = duk_del_prop_string(ctx, 1, "2"); printf("delete arr['2'] -> rc=%d\n", (int) rc); duk_json_encode(ctx, 0); printf("final object: %s\n", duk_to_string(ctx, 0)); duk_json_encode(ctx, 1); printf("final array: %s\n", duk_to_string(ctx, 1)); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static int NativeServiceObjectFinalizer(duk_context* ctx) { const char* peer; SessionInfo* sessionInfo; AJ_InfoPrintf(("ServiceObjectFinalizer\n")); duk_get_prop_string(ctx, 0, "dest"); if (!duk_is_undefined(ctx, -1)) { peer = duk_get_string(ctx, -1); if (peer) { AJS_GetGlobalStashObject(ctx, "sessions"); duk_get_prop_string(ctx, -1, peer); duk_get_prop_string(ctx, -1, "info"); sessionInfo = duk_get_buffer(ctx, -1, NULL); duk_pop_2(ctx); AJ_ASSERT(sessionInfo->refCount != 0); if ((--sessionInfo->refCount == 0) && sessionInfo->sessionId) { duk_del_prop_string(ctx, -1, peer); (void) AJ_BusLeaveSession(AJS_GetBusAttachment(), sessionInfo->sessionId); sessionInfo->sessionId = 0; } duk_pop(ctx); } /* * There is no guarantee that finalizers are only called once. This ensures that the * finalizer is idempotent. */ duk_del_prop_string(ctx, 0, "dest"); } duk_pop(ctx); return 0; }
// Assumes nargs are the top of the stack. Rest comes from request // Return value is not left on the stack. void duv_resolve(uv_req_t* req, int nargs) { duk_context *ctx = req->data; duv_push_handle(ctx, req); // stack: args... obj duk_get_prop_string(ctx, -1, "\xff""uv-callback"); // stack: args... obj callback duk_del_prop_string(ctx, -2, "\xff""uv-callback"); // stack: args... obj callback if (!duk_is_function(ctx, -1)) { // stack: args... obj callback duk_pop_n(ctx, 2 + nargs); return; } duk_remove(ctx, -2); // stack: args... callback duk_insert(ctx, -(nargs + 1)); // stack: callback args... duk_call(ctx, nargs); // stack: result duk_pop(ctx); // Remove the request from the GC roots duv_remove_handle(ctx, req); }
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); }
/* duk_del_prop_string(), non-configurable virtual property of a plain string. * Same behavior when called inside/outside of a Duktape/C activation. */ static duk_ret_t test_delpropstring_d(duk_context *ctx) { duk_ret_t rc; prep(ctx); rc = duk_del_prop_string(ctx, 2, "length"); printf("delete 'test_string'.length -> rc=%d\n", (int) rc); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* duk_del_prop_string(), DUK_INVALID_INDEX */ int test_2e(duk_context *ctx) { int rc; prep(ctx); rc = duk_del_prop_string(ctx, DUK_INVALID_INDEX, "foo"); printf("delete obj.foo -> rc=%d\n", rc); printf("final top: %d\n", duk_get_top(ctx)); return 0; }
/* duk_del_prop_string(), non-configurable virtual property of a plain string, * called from inside a Duktape/C context. */ int test_2c(duk_context *ctx) { int rc; prep(ctx); rc = duk_del_prop_string(ctx, 2, "5"); printf("delete 'test_string'['5'] -> rc=%d\n", rc); printf("final top: %d\n", duk_get_top(ctx)); return 0; }
/* duk_del_prop_string(), non-configurable property (array 'length' property), * called from inside a Duktape/C context. */ int test_2b(duk_context *ctx) { int rc; prep(ctx); rc = duk_del_prop_string(ctx, 1, "length"); printf("delete arr.length -> rc=%d\n", rc); printf("final top: %d\n", duk_get_top(ctx)); return 0; }
/* duk_del_prop_string(), invalid index */ static duk_ret_t test_2e(duk_context *ctx) { duk_ret_t rc; prep(ctx); rc = duk_del_prop_string(ctx, 234, "foo"); printf("delete obj.foo -> rc=%d\n", (int) rc); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* duk_del_prop_string(), non-configurable virtual property of a plain string. * Same behavior when called inside/outside of a Duktape/C activation. */ static duk_ret_t test_2c(duk_context *ctx) { duk_ret_t rc; prep(ctx); rc = duk_del_prop_string(ctx, 2, "5"); printf("delete 'test_string'['5'] -> rc=%d\n", (int) rc); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* duk_del_prop_string(), not object coercible */ static duk_ret_t test_delpropstring_g(duk_context *ctx) { duk_ret_t rc; duk_set_top(ctx, 0); duk_push_null(ctx); rc = duk_del_prop_string(ctx, -1, "foo"); printf("delete null.foo -> rc=%d\n", (int) rc); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* duk_del_prop_string(), DUK_INVALID_INDEX */ static duk_ret_t test_delpropstring_f(duk_context *ctx) { duk_ret_t rc; prep(ctx); rc = duk_del_prop_string(ctx, DUK_INVALID_INDEX, "foo"); printf("delete obj.foo -> rc=%d\n", (int) rc); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static duk_ret_t test_delete2(duk_context *ctx, void *udata) { duk_ret_t rc; (void) udata; setup_proxy(ctx); printf("top: %ld\n", (long) duk_get_top(ctx)); rc = duk_del_prop_string(ctx, -1, "_deleteTest"); printf("delete result: rc=%d\n", (int) rc); duk_pop(ctx); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
static int es_native_finalizer(duk_context *ctx) { int type = duk_get_current_magic(ctx); duk_get_prop_string(ctx, 0, PTRNAME); if(duk_is_pointer(ctx, -1)) { void *ptr = duk_get_pointer(ctx, -1); duk_del_prop_string(ctx, 0, PTRNAME); call_finalizer(type, ptr); duk_pop(ctx); } return 0; }
int w_LocalStorage_prototype_removeItem(duk_context *ctx) { const char *key = duk_require_string(ctx, 0); duk_push_this(ctx); /* this */ duk_get_prop_string(ctx, -1, "__MURAL_DATA__"); /* this, __MURAL_DATA__ */ duk_del_prop_string(ctx, -1, key); /* this, __MURAL_DATA__ */ LocalStorage *inst = getNativePointer<LocalStorage>(ctx); duk_json_encode(ctx, -1); /* this, JSON(__MURAL_DATA__) */ inst->setData(duk_to_string(ctx, -1)); /* this, string(JSON(__MURAL_DATA__)) */ duk_pop_2(ctx); return 0; }
static int ClearTriggerCallback(duk_context* ctx, int pinFunc, AJS_IO_PinTriggerCondition condition) { int32_t trigId; AJS_TargetIO_PinDisableTrigger(PinCtxPtr(ctx), pinFunc, condition, &trigId); if (trigId != AJS_IO_PIN_NO_TRIGGER) { duk_get_global_string(ctx, AJS_IOObjectName); duk_get_prop_string(ctx, -1, AJS_HIDDEN_PROP("trigs")); duk_del_prop_index(ctx, -1, trigId); duk_pop_2(ctx); } duk_push_this(ctx); duk_del_prop_string(ctx, -1, "trigger"); /* * Leave pin object on the stack */ return 1; }
AJ_Status AJS_HandleAcceptSession(duk_context* ctx, AJ_Message* msg, uint16_t port, uint32_t sessionId, const char* joiner) { uint32_t accept = TRUE; SessionInfo* sessionInfo; /* * Create an entry in the sessions table so we can track this peer */ AJS_GetGlobalStashObject(ctx, "sessions"); sessionInfo = AllocSessionObject(ctx, joiner); /* * If there is no handler automatically accept the connection */ AJS_GetAllJoynProperty(ctx, "onPeerConnected"); if (duk_is_callable(ctx, -1)) { /* Empty interface array */ duk_push_array(ctx); AddServiceObject(ctx, sessionInfo, "/", joiner); if (AJS_DebuggerIsAttached()) { msg = AJS_CloneAndCloseMessage(ctx, msg); } if (duk_pcall(ctx, 1) != DUK_EXEC_SUCCESS) { AJS_ConsoleSignalError(ctx); accept = FALSE; } else { accept = duk_get_boolean(ctx, -1); } } duk_pop_2(ctx); /* * It is possible that we already have an outbound session to this peer so if we are not * accepting the session we can only delete the entry if the refCount is zero. */ if (accept) { ++sessionInfo->refCount; sessionInfo->port = port; sessionInfo->sessionId = sessionId; } else if (sessionInfo->refCount == 0) { duk_del_prop_string(ctx, -1, joiner); } /* Pop sessions object */ duk_pop(ctx); return AJ_BusReplyAcceptSession(msg, accept); }
void duv_emit(uv_handle_t* handle, const char* key, int nargs, int cleanup) { duk_context *ctx = handle->data; duv_push_handle(ctx, handle); // stack: args... this duk_get_prop_string(ctx, -1, key); // stack: args... this fn if (cleanup) duk_del_prop_string(ctx, -2, key); // stack: args... this fn if (!duk_is_function(ctx, -1)) { duk_pop_n(ctx, 2 + nargs); return; } duk_insert(ctx, -(nargs + 2)); // stack: fn args... this duk_insert(ctx, -(nargs + 1)); // stack: fn this args... duk_call_method(ctx, nargs); // stack: result duk_pop(ctx); }
/* duk_del_prop_string(), success cases */ int test_2a(duk_context *ctx) { int rc; prep(ctx); rc = duk_del_prop_string(ctx, 0, "foo"); printf("delete obj.foo -> rc=%d\n", rc); rc = duk_del_prop_string(ctx, 0, "nonexistent"); printf("delete obj.nonexistent -> rc=%d\n", rc); rc = duk_del_prop_string(ctx, 0, "123"); printf("delete obj['123'] -> rc=%d\n", rc); rc = duk_del_prop_string(ctx, 1, "nonexistent"); printf("delete arr.nonexistent -> rc=%d\n", rc); rc = duk_del_prop_string(ctx, 1, "2"); printf("delete arr['2'] -> rc=%d\n", rc); /* non-configurable property, but running in non-strict mode */ rc = duk_del_prop_string(ctx, 1, "length"); printf("delete arr.length -> rc=%d\n", rc); /* non-configurable property, but running in non-strict mode */ rc = duk_del_prop_string(ctx, 2, "5"); printf("delete 'test_string'['5'] -> rc=%d\n", rc); rc = duk_del_prop_string(ctx, 2, "length"); printf("delete 'test_string'.length -> rc=%d\n", rc); duk_json_encode(ctx, 0); printf("final object: %s\n", duk_to_string(ctx, 0)); duk_json_encode(ctx, 1); printf("final array: %s\n", duk_to_string(ctx, 1)); printf("final top: %d\n", duk_get_top(ctx)); return 0; }
/* * Delete session info object pointed to by sessionId. If sessionId * is zero then delete all session info objects. */ static AJ_Status RemoveSessions(duk_context* ctx, uint32_t sessionId) { AJ_Status status = AJ_OK; AJS_GetGlobalStashObject(ctx, "sessions"); duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY); while (duk_next(ctx, -1, 1)) { SessionInfo* sessionInfo; const char* peer = duk_get_string(ctx, -2); duk_get_prop_string(ctx, -1, "info"); sessionInfo = duk_get_buffer(ctx, -1, NULL); duk_pop_3(ctx); if (sessionId == 0) { AJ_InfoPrintf(("RemoveSessions(): Leaving session: %u\n", sessionInfo->sessionId)); status = AJ_BusLeaveSession(AJS_GetBusAttachment(), sessionInfo->sessionId); } else if (sessionInfo->sessionId == sessionId) { status = AJ_BusLeaveSession(AJS_GetBusAttachment(), sessionInfo->sessionId); duk_del_prop_string(ctx, -2, peer); break; } } duk_pop_2(ctx); /* * TODO - this is not all that useful because it only indicates that a peer has gone away * without being able to specify exactly which services are affected. The problem is we cannot * hold a reference to the service object because we a relying on the service object finalizer * to clean up sessions that are no longer in use. If we hold a reference the finalizer will * never get called. */ if (sessionId != 0) { AJS_GetAllJoynProperty(ctx, "onPeerDisconnected"); if (duk_is_callable(ctx, -1)) { if (duk_pcall(ctx, 0) != DUK_EXEC_SUCCESS) { AJS_ConsoleSignalError(ctx); } } duk_pop(ctx); } else { AJS_ClearGlobalStashObject(ctx, "sessions"); } return status; }
void _gum_duk_unprotect (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); g_assert (!duk_is_undefined (ctx, -1)); duk_get_prop_string (ctx, -1, "n"); ref_count = duk_get_uint (ctx, -1); duk_pop (ctx); ref_count--; if (ref_count == 0) { duk_pop (ctx); duk_del_prop_string (ctx, -1, name); } else { duk_push_uint (ctx, ref_count); duk_put_prop_string (ctx, -2, "n"); duk_pop (ctx); } duk_pop (ctx); }
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; }
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; }
AJ_Status AJS_FoundAdvertisedName(duk_context* ctx, AJ_Message* msg) { AJ_Status status; SessionInfo* sessionInfo; const char* name; const char* prefix; uint16_t port; uint16_t mask; duk_idx_t fnmeIdx; duk_idx_t sessIdx; AJ_UnmarshalArgs(msg, "sqs", &name, &mask, &prefix); AJ_InfoPrintf(("AJS_FoundAdvertisedName %s %s\n", prefix, name)); AJS_GetGlobalStashObject(ctx, "sessions"); sessIdx = duk_get_top_index(ctx); /* * If the name is already listed we can ignore this signal */ if (duk_get_prop_string(ctx, -1, name)) { duk_pop_2(ctx); return AJ_OK; } /* * Check if there is a callback registered for this name (actually the prefix) */ AJS_GetGlobalStashObject(ctx, "findName"); duk_get_prop_string(ctx, -1, prefix); if (duk_is_undefined(ctx, -1)) { duk_pop_3(ctx); return AJ_OK; } fnmeIdx = duk_get_top_index(ctx); /* * From here on we make it look like we received an announcement */ port = AJS_GetIntProp(ctx, fnmeIdx, "port"); status = AJ_BusJoinSession(msg->bus, name, port, NULL); if (status == AJ_OK) { duk_idx_t anIdx; duk_idx_t svcIdx; /* * Create service object and add it to the sessions array */ svcIdx = duk_push_object(ctx); /* * Set the "info" and "anno" properties on the service object */ sessionInfo = duk_push_fixed_buffer(ctx, sizeof(SessionInfo)); sessionInfo->port = port; duk_put_prop_string(ctx, svcIdx, "info"); anIdx = duk_push_array(ctx); /* * Push array of interfaces */ duk_get_prop_string(ctx, fnmeIdx, "interfaces"); /* * Set the callback to be called when the JOIN_SESSION reply is received */ AJS_GetGlobalStashObject(ctx, "serviceCB"); duk_get_prop_index(ctx, -2, 0); /* key - first interface in "interfaces" array */ duk_get_prop_string(ctx, fnmeIdx, "cb"); /* val - callback function */ duk_put_prop(ctx, -3); duk_pop(ctx); /* Interfaces array is at the top of the stack */ AddServiceObject(ctx, sessionInfo, AJS_GetStringProp(ctx, fnmeIdx, "path"), name); /* * Append service object to the announcements array for processing later */ duk_put_prop_index(ctx, anIdx, duk_get_length(ctx, anIdx)); AJ_ASSERT(duk_get_top_index(ctx) == anIdx); duk_put_prop_string(ctx, svcIdx, "anno"); AJ_ASSERT(duk_get_top_index(ctx) == svcIdx); duk_put_prop_string(ctx, sessIdx, name); /* * Store the reply serial number for the JOIN_SESSION reply */ sessionInfo->replySerial = msg->bus->serial - 1; } else { duk_del_prop_string(ctx, sessIdx, prefix); } /* Clean up the stack */ duk_pop_n(ctx, 4); return status; }
/* * Announcements are processed as follows: * * 1) First check if there is an existing session for the announcement sender. If not create and * initialize an empty session object. A session object has two properties "info" which is a buffer * that holds state information about the session and an array "anno" which is used to accumulate * announcements from the same sender. * 2) Unmarshal the announcement signal and if there is a callback registered for any of the * interfaces add a service object to announcements array. The "interfaces" property on the * service object is an array of all interfaces in the announcement. * 3) If there is already a session with the announcement sender make callbacks now, otherwise send * a JOIN_SESSION method call to join the session with the announcement sender. The callbacks are * reevaluated when the JOIN_SESSION reply is received. */ AJ_Status AJS_AboutAnnouncement(duk_context* ctx, AJ_Message* msg) { AJ_BusAttachment* aj = msg->bus; AJ_Status status; uint16_t version; duk_idx_t anIdx; SessionInfo* sessionInfo; AJ_Arg objList; const char* sender; AJ_InfoPrintf(("AJS_AboutAnnouncement\n")); /* * Ignore our own announcements */ if (strcmp(msg->sender, AJ_GetUniqueName(msg->bus)) == 0) { AJ_InfoPrintf(("Ignoring our own announcement\n")); return AJ_OK; } /* * Push the sender string on the stack to stabilize it. */ sender = duk_push_string(ctx, msg->sender); /* * Announcements are accumulated in a global stash object indexed by peer name */ AJS_GetGlobalStashObject(ctx, "sessions"); sessionInfo = AllocSessionObject(ctx, sender); /* * Get the announcements array */ duk_get_prop_string(ctx, -1, "anno"); anIdx = duk_get_top_index(ctx); /* * Find out if there are JavaScript handlers for this announcement */ AJ_UnmarshalArgs(msg, "qq", &version, &sessionInfo->port); status = AJ_UnmarshalContainer(msg, &objList, AJ_ARG_ARRAY); while (status == AJ_OK) { int ifcCount = 0; uint8_t hasCB = FALSE; AJ_Arg obj; AJ_Arg interfaces; const char* path; status = AJ_UnmarshalContainer(msg, &obj, AJ_ARG_STRUCT); if (status != AJ_OK) { break; } AJ_UnmarshalArgs(msg, "o", &path); /* * Array to accumulate interfaces */ duk_push_array(ctx); status = AJ_UnmarshalContainer(msg, &interfaces, AJ_ARG_ARRAY); while (status == AJ_OK) { /* * Accumulate interfaces checking */ const char* iface; status = AJ_UnmarshalArgs(msg, "s", &iface); if (status == AJ_OK) { duk_push_string(ctx, iface); duk_put_prop_index(ctx, -2, ifcCount++); } /* * We will disard the interface array if no callbacks are registered. */ if (!hasCB && HasServiceCallback(ctx, iface)) { hasCB = TRUE; } } if (hasCB) { AddServiceObject(ctx, sessionInfo, path, sender); /* * Append service object to the announcements array for processing later */ duk_put_prop_index(ctx, anIdx, duk_get_length(ctx, anIdx)); } else { /* discard the interface array */ duk_pop(ctx); } if (status == AJ_ERR_NO_MORE) { status = AJ_UnmarshalCloseContainer(msg, &interfaces); } if (status == AJ_OK) { status = AJ_UnmarshalCloseContainer(msg, &obj); } } if (status == AJ_ERR_NO_MORE) { status = AJ_UnmarshalCloseContainer(msg, &objList); } /* * All done with this message - we need to close it now to free up the output buffer before we * attempt to call AJ_BusJoinSession() below. */ AJ_CloseMsg(msg); /* Pop "anno" and the session object */ duk_pop_2(ctx); if (status != AJ_OK) { goto Exit; } /* * If we already have a session with this peer callback with the new service objects */ if (sessionInfo->sessionId) { AnnouncementCallbacks(ctx, sender, sessionInfo); goto Exit; } /* * If there is a JOIN_SESSION in progress we have nothing more to do */ if (sessionInfo->replySerial != 0) { goto Exit; } /* * If announcements were registered attempt to join the session */ if (sessionInfo->refCount) { status = AJ_BusJoinSession(aj, sender, sessionInfo->port, NULL); if (status == AJ_OK) { sessionInfo->replySerial = aj->serial - 1; } } /* * At this point if we don't have a JOIN_SESSION in progress we must delete the session object */ if (sessionInfo->replySerial == 0) { duk_del_prop_string(ctx, -2, sender); } Exit: /* Pop "sessions" and sender string */ duk_pop_2(ctx); return status; }
static void duk__del_cached_module(duk_context *ctx, const char *id) { duk_push_global_stash(ctx); (void) duk_get_prop_string(ctx, -1, "\xff" "requireCache"); duk_del_prop_string(ctx, -1, id); duk_pop_2(ctx); }
void duv_remove_handle(duk_context *ctx, void *handle) { duk_push_heap_stash(ctx); snprintf(key, KEYLEN, "%"PRIXPTR, (uintptr_t)handle); duk_del_prop_string(ctx, -1, key); duk_pop(ctx); }
//duk_bool_t duk_del_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key); duk_bool_t aperl_duk_del_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key) { duk_bool_t ret = duk_del_prop_string(ctx, obj_index, key); return ret; }