static int NativeIoSpi(duk_context* ctx) { uint32_t mosi, miso, cs, clk, clock; uint8_t master; uint8_t cpol, cpha, data; void* spiCtx; AJ_Status status; int idx; mosi = GetPinId(ctx, 0, AJS_IO_FUNCTION_SPI_MOSI); miso = GetPinId(ctx, 1, AJS_IO_FUNCTION_SPI_MISO); cs = GetPinId(ctx, 2, AJS_IO_FUNCTION_SPI_SS); clk = GetPinId(ctx, 3, AJS_IO_FUNCTION_SPI_SCK); duk_get_prop_string(ctx, 4, "clock"); clock = duk_require_int(ctx, -1); duk_get_prop_string(ctx, 4, "master"); if (duk_get_boolean(ctx, -1)) { master = TRUE; } else { master = FALSE; } duk_get_prop_string(ctx, 4, "cpol"); cpol = duk_require_int(ctx, -1); duk_get_prop_string(ctx, 4, "cpha"); cpha = duk_require_int(ctx, -1); duk_get_prop_string(ctx, 4, "data"); data = duk_require_int(ctx, -1); duk_pop(ctx); // Clock polarity must be high (1) or low (0) if (cpol != 0 && cpol != 1) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "cpol must be 0 or 1; you gave %u", cpol); } // Clock edge phase must be one edge (0) or two edge (1) if (cpha != 0 && cpha != 1) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "cpha must be 0 or 1; you gave %u", cpha); } // Data bits must be 8, 16, or 32 if (data != 8 && data != 16 && data != 32) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "data bits must be 8, 16 or 32; you gave %u", data); } status = AJS_TargetIO_SpiOpen(mosi, miso, cs, clk, clock, master, cpol, cpha, data, &spiCtx); if (status != AJ_OK) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Failed to open SPI device"); } idx = NewIOObject(ctx, spiCtx, AJS_IO_FUNCTION_SPI, NativeSpiFinalizer); duk_push_c_lightfunc(ctx, NativeSpiRead, 1, 0, 0); duk_put_prop_string(ctx, idx, "read"); duk_push_c_lightfunc(ctx, NativeSpiWrite, 1, 0, 0); duk_put_prop_string(ctx, idx, "write"); return 1; }
duk_idx_t AJS_UnmarshalMessage(duk_context* ctx, AJ_Message* msg, uint8_t accessor) { duk_idx_t objIndex = duk_push_object(ctx); duk_push_string(ctx, msg->sender); duk_put_prop_string(ctx, objIndex, "sender"); if ((msg->hdr->msgType == AJ_MSG_METHOD_CALL) || (msg->hdr->msgType == AJ_MSG_SIGNAL)) { duk_push_string(ctx, msg->member); duk_put_prop_string(ctx, objIndex, "member"); duk_push_string(ctx, msg->iface); duk_put_prop_string(ctx, objIndex, "iface"); duk_push_string(ctx, msg->objPath); duk_put_prop_string(ctx, objIndex, "path"); /* If sender == unique name then true, otherwise false */ duk_push_boolean(ctx, !abs(strcmp(msg->sender, AJS_GetBusAttachment()->uniqueName))); duk_put_prop_string(ctx, objIndex, "fromSelf"); if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) { AJS_ReplyInternal* msgReply; /* * Private read-only information needed for composing the reply */ duk_push_string(ctx, AJS_HIDDEN_PROP("reply")); msgReply = duk_push_fixed_buffer(ctx, sizeof(AJS_ReplyInternal)); msgReply->msgId = msg->msgId; msgReply->flags = msg->hdr->flags; msgReply->serialNum = msg->hdr->serialNum; msgReply->sessionId = msg->sessionId; msgReply->accessor = accessor; duk_def_prop(ctx, objIndex, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE); /* * Register the reply functions */ duk_push_c_lightfunc(ctx, AJS_MethodCallReply, DUK_VARARGS, 0, 0); duk_put_prop_string(ctx, objIndex, "reply"); duk_push_c_lightfunc(ctx, AJS_MethodCallError, DUK_VARARGS, 0, 0); duk_put_prop_string(ctx, objIndex, "errorReply"); } } else { int isError = (msg->hdr->msgType == AJ_MSG_ERROR); AJ_InfoPrintf(("Reply serial %d\n", msg->replySerial)); duk_push_int(ctx, msg->replySerial); duk_put_prop_string(ctx, objIndex, "replySerial"); if (isError) { duk_push_string(ctx, msg->error); duk_put_prop_string(ctx, objIndex, "error"); } duk_push_boolean(ctx, isError); duk_put_prop_string(ctx, objIndex, "isErrorReply"); } return objIndex; }
static duk_ret_t test_2a(duk_context *ctx, void *udata) { (void) udata; /* Test first with nothing on stack index -3. */ duk_safe_call(ctx, test__null, NULL, 0, 1); printf("%s\n", duk_safe_to_string(ctx, 0)); duk_pop(ctx); duk_set_top(ctx, 0); duk_push_undefined(ctx); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_null(ctx); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_true(ctx); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_false(ctx); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_number(ctx, 123.0); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_string(ctx, "foo\x00" "bar"); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_fixed_buffer(ctx, 16); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_pointer(ctx, NULL); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_pointer(ctx, (void *) 0xdeadbeefUL); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_object(ctx); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_array(ctx); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_c_function(ctx, dummy_func, 0); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_c_lightfunc(ctx, dummy_func, 0, 0, 0); test__require_calls(ctx); duk_set_top(ctx, 0); duk_eval_string(ctx, "(function dummy(){})"); test__require_calls(ctx); duk_set_top(ctx, 0); duk_push_thread(ctx); test__require_calls(ctx); printf("done\n"); return 0; }
static duk_ret_t test_1(duk_context *ctx, void *udata) { (void) udata; /* Lightfunc, must sanitize the address for the expect string. */ duk_eval_string(ctx, "(function (v) {\n" " print(String(v).replace(/light_[0-9a-fA-f_]+/, 'light_PTR'));\n" "})"); duk_push_c_lightfunc(ctx, dummy_func, 0 /*nargs*/, 0 /*length*/, 0 /*magic*/); duk_call(ctx, 1); duk_pop(ctx); /* Function with a .name containing invalid characters. * * This is not currently handled very well: the .toString() output * uses the name as is, which can make the output technically * incorrect because it won't parse as a function. However, the * only way to create such functions is from C code so this is a * minor issue. */ duk_push_c_function(ctx, dummy_func, 0 /*nargs*/); duk_push_string(ctx, "name"); duk_push_string(ctx, "dummy {"); duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE); printf("%s\n", duk_to_string(ctx, -1)); duk_pop(ctx); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
/* * Configures a pin as a digital output pin */ static int NativeIoDigitalOut(duk_context* ctx) { AJ_Status status; int idx; void* pinCtx; uint32_t pin = GetPinId(ctx, 0, AJS_IO_FUNCTION_DIGITAL_OUT); /* * Target specific I/O pin initialization */ status = AJS_TargetIO_PinOpen(pin, AJS_IO_PIN_OUTPUT, &pinCtx); if (status != AJ_OK) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Failed to configure digital output pin: %s", AJ_StatusText(status)); } idx = NewIOObject(ctx, pinCtx, AJS_IO_FUNCTION_DIGITAL_OUT, NativePinFinalizer); /* * Functions to set/get the pin level */ AJS_SetPropertyAccessors(ctx, idx, "level", NativeLevelSetter, NativeLevelGetter); /* * Check for and set initial value */ if (duk_is_number(ctx, 1)) { duk_dup(ctx, 1); duk_put_prop_string(ctx, idx, "level"); } /* * Toggle function */ duk_push_c_lightfunc(ctx, NativeTogglePin, 0, 0, 0); duk_put_prop_string(ctx, idx, "toggle"); /* * Only allow if the PWM functions is supported */ if (AJS_TargetIO_GetInfo(pin)->functions & AJS_IO_FUNCTION_PWM) { duk_push_c_lightfunc(ctx, NativePWM, 2, 0, 0); duk_put_prop_string(ctx, idx, "pwm"); } /* * Return the digital output pin object */ return 1; }
static int NativeIoUart(duk_context* ctx) { AJ_Status status; uint8_t tx, rx; uint32_t baud; void* uartCtx = NULL; int32_t rxTrigId; int idx; tx = GetPinId(ctx, 0, AJS_IO_FUNCTION_UART_TX); rx = GetPinId(ctx, 1, AJS_IO_FUNCTION_UART_RX); baud = duk_require_int(ctx, 2); duk_pop(ctx); status = AJS_TargetIO_UartOpen(tx, rx, baud, &uartCtx, &rxTrigId); if (status != AJ_OK) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Failed to open UART device\n"); } idx = NewIOObject(ctx, uartCtx, AJS_IO_FUNCTION_UART, NativeUartFinalizer); duk_push_c_lightfunc(ctx, NativeUartSetTrigger, 2, 0, 0); duk_put_prop_string(ctx, idx, "setTrigger"); duk_push_c_lightfunc(ctx, NativeUartClearTrigger, 1, 0, 0); duk_put_prop_string(ctx, idx, "clearTrigger"); duk_push_c_lightfunc(ctx, NativeUartRead, 2, 0, 0); duk_put_prop_string(ctx, idx, "read"); duk_push_c_lightfunc(ctx, NativeUartWrite, 1, 0, 0); duk_put_prop_string(ctx, idx, "write"); /* * Add the uart I/O object to the triggers array. */ duk_get_global_string(ctx, AJS_IOObjectName); duk_get_prop_string(ctx, -1, AJS_HIDDEN_PROP("trigs")); duk_dup(ctx, idx); duk_put_prop_index(ctx, -2, rxTrigId); duk_pop_2(ctx); return 1; }
static int NativeGetAllProps(duk_context* ctx) { if (!duk_is_string(ctx, 0)) { duk_error(ctx, DUK_ERR_TYPE_ERROR, "First argument must be the interface name"); } duk_push_c_lightfunc(ctx, AJS_MarshalMethodCall, 1, 0, 0); duk_push_this(ctx); MessageSetup(ctx, &AJ_PropertiesIface[0][1], "GetAll", NULL, AJ_MSG_METHOD_CALL); duk_dup(ctx, 0); duk_call_method(ctx, 1); return 1; }
/* * Configures a pin as a digital input pin */ static int NativeIoDigitalIn(duk_context* ctx) { AJ_Status status; int idx; void* pinCtx; int config = -1; uint32_t pin = GetPinId(ctx, 0, AJS_IO_FUNCTION_DIGITAL_IN); if (duk_is_undefined(ctx, 1)) { config = AJS_IO_PIN_PULL_UP; } else if (duk_is_number(ctx, 1)) { config = (AJS_IO_PinConfig)duk_get_int(ctx, 1); } if ((config != AJS_IO_PIN_OPEN_DRAIN) && (config != AJS_IO_PIN_PULL_UP) && (config != AJS_IO_PIN_PULL_DOWN)) { duk_error(ctx, DUK_ERR_RANGE_ERROR, "Configuration must be pullUp, pullDown, or openDrain"); } /* * Target specific I/O pin initialization */ status = AJS_TargetIO_PinOpen(pin, (AJS_IO_PinConfig)config, &pinCtx); if (status != AJ_OK) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Failed to open digital input pin: %s", AJ_StatusText(status)); } idx = NewIOObject(ctx, pinCtx, AJS_IO_FUNCTION_DIGITAL_IN, NativePinFinalizer); /* * Function to get the pin level */ AJS_SetPropertyAccessors(ctx, idx, "level", NULL, NativeLevelGetter); /* * Function to set and clear a trigger */ duk_push_c_lightfunc(ctx, NativePinSetTrigger, 3, 0, 0); duk_put_prop_string(ctx, idx, "setTrigger"); duk_push_c_lightfunc(ctx, NativePinClearTrigger, 1, 0, 0); duk_put_prop_string(ctx, idx, "clearTrigger"); /* * Return the digital input pin object */ return 1; }
static int NativeIoI2cSlave(duk_context* ctx) { AJ_Status status; void* i2cCtx; uint8_t sda = GetPinId(ctx, 0, AJS_IO_FUNCTION_I2C_SDA); uint8_t scl = GetPinId(ctx, 1, AJS_IO_FUNCTION_I2C_SCL); uint8_t address = duk_require_int(ctx, 2); status = AJS_TargetIO_I2cOpen(sda, scl, 0, AJS_I2C_MODE_SLAVE, address, &i2cCtx); if (status != AJ_OK) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Failed to open I2C device in slave mode\n"); } NewIOObject(ctx, i2cCtx, AJS_IO_FUNCTION_I2C_SLAVE, NativeI2cFinalizer); duk_push_c_lightfunc(ctx, NativeI2cTransfer, 3, 0, 0); duk_put_prop_string(ctx, -2, "transfer"); return 1; }
static int NativeGetProp(duk_context* ctx) { const char* prop; const char* iface; if (!duk_is_string(ctx, 0)) { duk_error(ctx, DUK_ERR_TYPE_ERROR, "Lone argument must be the property name"); } duk_push_c_lightfunc(ctx, AJS_MarshalMethodCall, 2, 0, 0); prop = duk_get_string(ctx, 0); duk_push_this(ctx); iface = FindInterfaceForMember(ctx, 0, &prop); MessageSetup(ctx, &AJ_PropertiesIface[0][1], "Get", NULL, AJ_MSG_METHOD_CALL); duk_push_string(ctx, iface); duk_push_string(ctx, prop); duk_call_method(ctx, 2); return 1; }
static int NativeIoI2cMaster(duk_context* ctx) { AJ_Status status; uint32_t clock = 0; void* i2cCtx; uint8_t sda = GetPinId(ctx, 0, AJS_IO_FUNCTION_I2C_SDA); uint8_t scl = GetPinId(ctx, 1, AJS_IO_FUNCTION_I2C_SCL); if (!duk_is_undefined(ctx, 2)) { clock = duk_require_int(ctx, 2); } status = AJS_TargetIO_I2cOpen(sda, scl, clock, AJS_I2C_MODE_MASTER, 0, &i2cCtx); if (status != AJ_OK) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Failed to open I2C device in master mode\n"); } NewIOObject(ctx, i2cCtx, AJS_IO_FUNCTION_I2C_MASTER, NativeI2cFinalizer); duk_push_c_lightfunc(ctx, NativeI2cTransfer, 3, 0, 0); duk_put_prop_string(ctx, -2, "transfer"); return 1; }
/* * Signals require an object path, interface, and member */ static void InitSignal(duk_context* ctx, const char* dest, uint32_t session) { const char* path = duk_require_string(ctx, 0); const char* iface; const char* member; /* * Build up a dummy service object */ duk_push_object(ctx); /* * Get the interfaces from the object path */ AJS_GetAllJoynProperty(ctx, "objectDefinition"); duk_get_prop_string(ctx, -1, path); if (duk_is_undefined(ctx, -1)) { duk_error(ctx, DUK_ERR_TYPE_ERROR, "Unknown object path '%s'", path); } duk_get_prop_string(ctx, -1, "interfaces"); duk_put_prop_string(ctx, -4, "interfaces"); duk_pop_2(ctx); /* * Set the endpoint information */ duk_push_string(ctx, dest); duk_put_prop_string(ctx, -2, "dest"); duk_push_number(ctx, session); duk_put_prop_string(ctx, -2, "session"); /* * Resolve the interface name */ iface = FindInterfaceForMember(ctx, 1, &member); MessageSetup(ctx, iface, member, path, AJ_MSG_SIGNAL); duk_dup(ctx, 0); duk_put_prop_string(ctx, -2, "path"); duk_push_c_lightfunc(ctx, AJS_MarshalSignal, DUK_VARARGS, 0, 0); duk_put_prop_string(ctx, -2, "send"); }
static int NativeMethod(duk_context* ctx) { const char* member; const char* iface; duk_push_this(ctx); iface = FindInterfaceForMember(ctx, 0, &member); if (!iface || !member) { duk_error(ctx, DUK_ERR_TYPE_ERROR, "Interface or member are null\n"); return 0; } AJ_InfoPrintf(("NativeMethod %s\n", member)); MessageSetup(ctx, iface, member, NULL, AJ_MSG_METHOD_CALL); /* * Register function for actually calling this method */ duk_push_c_lightfunc(ctx, AJS_MarshalMethodCall, DUK_VARARGS, 0, 0); duk_put_prop_string(ctx, -2, "call"); /* return the method object we just created */ return 1; }
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; }
//duk_idx_t duk_push_c_lightfunc(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_idx_t length, duk_int_t magic); duk_idx_t aperl_duk_push_c_lightfunc(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_idx_t length, duk_int_t magic) { duk_idx_t ret = duk_push_c_lightfunc(ctx, func, nargs, length, magic); return ret; }
static AJ_Status HandleMessage(duk_context* ctx, duk_idx_t ajIdx, AJ_Message* msg) { duk_idx_t msgIdx; uint8_t accessor = AJS_NOT_ACCESSOR; const char* func; AJ_Status status; uint8_t ldstate; #if !defined(AJS_CONSOLE_LOCKDOWN) status = AJS_GetLockdownState(&ldstate); if (status == AJ_OK && ldstate == AJS_CONSOLE_UNLOCKED) { status = AJS_ConsoleMsgHandler(ctx, msg); if (status != AJ_ERR_NO_MATCH) { if (status != AJ_OK) { AJ_WarnPrintf(("AJS_ConsoleMsgHandler returned %s\n", AJ_StatusText(status))); } return status; } } #endif /* * JOIN_SESSION replies are handled in the AllJoyn.js layer and don't get passed to JavaScript */ if (msg->msgId == AJ_REPLY_ID(AJ_METHOD_JOIN_SESSION)) { return AJS_HandleJoinSessionReply(ctx, msg); } /* * Nothing more to do if the AllJoyn module was not loaded */ if (ajIdx < 0) { return AJ_OK; } /* * Let the bases services layer take a look at the message */ status = AJS_ServicesMsgHandler(msg); if (status != AJ_ERR_NO_MATCH) { if (status != AJ_OK) { AJ_WarnPrintf(("AJS_ServicesMsgHandler returned %s\n", AJ_StatusText(status))); } return status; } /* * Push the appropriate callback function onto the duktape stack */ if (msg->hdr->msgType == AJ_MSG_SIGNAL) { /* * About announcements and found name signal get special handling */ if (msg->msgId == AJ_SIGNAL_ABOUT_ANNOUNCE) { return AJS_AboutAnnouncement(ctx, msg); } if (msg->msgId == AJ_SIGNAL_FOUND_ADV_NAME) { return AJS_FoundAdvertisedName(ctx, msg); } if ((msg->msgId == AJ_SIGNAL_SESSION_LOST) || (msg->msgId == AJ_SIGNAL_SESSION_LOST_WITH_REASON)) { if (AJS_DebuggerIsAttached()) { msg = AJS_CloneAndCloseMessage(ctx, msg); } return AJS_SessionLost(ctx, msg); } func = "onSignal"; duk_get_prop_string(ctx, ajIdx, func); } else if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) { accessor = IsPropAccessor(msg); switch (accessor) { case AJS_NOT_ACCESSOR: func = "onMethodCall"; break; case AJ_PROP_GET: func = "onPropGet"; break; case AJ_PROP_SET: func = "onPropSet"; break; case AJ_PROP_GET_ALL: func = "onPropGetAll"; break; default: return AJ_ERR_INVALID; } duk_get_prop_string(ctx, ajIdx, func); } else { func = "onReply"; AJS_GetGlobalStashObject(ctx, func); if (duk_is_object(ctx, -1)) { duk_get_prop_index(ctx, -1, msg->replySerial); duk_swap_top(ctx, -2); /* * Clear the onReply entry */ duk_del_prop_index(ctx, -1, msg->replySerial); duk_pop(ctx); } } /* * Skip if there is no function to call. */ if (!duk_is_function(ctx, -1)) { if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) { AJ_Message error; AJ_WarnPrintf(("%s: not registered - rejecting message\n", func)); AJ_MarshalErrorMsg(msg, &error, AJ_ErrRejected); status = AJ_DeliverMsg(&error); } else { AJ_WarnPrintf(("%s: not registered - ignoring message\n", func)); status = AJ_OK; } duk_pop(ctx); return status; } /* * Opens up a stack entry above the function */ duk_dup_top(ctx); msgIdx = AJS_UnmarshalMessage(ctx, msg, accessor); AJ_ASSERT(msgIdx == (ajIdx + 3)); /* * Save the message object on the stack */ duk_copy(ctx, msgIdx, -3); /* * Special case for GET prop so we can get the signature for marshalling the result */ if (accessor == AJS_NOT_ACCESSOR) { status = AJS_UnmarshalMsgArgs(ctx, msg); } else { status = AJS_UnmarshalPropArgs(ctx, msg, accessor, msgIdx); } if (status == AJ_OK) { duk_idx_t numArgs = duk_get_top(ctx) - msgIdx - 1; /* * If attached, the debugger will begin to unmarshal a message when the * method handler is called, therefore it must be cloned-and-closed now. */ if (AJS_DebuggerIsAttached()) { msg = AJS_CloneAndCloseMessage(ctx, msg); } if (duk_pcall_method(ctx, numArgs) != DUK_EXEC_SUCCESS) { const char* err = duk_safe_to_string(ctx, -1); AJ_ErrPrintf(("%s: %s\n", func, err)); /* * Generate an error reply if this was a method call */ if (msg->hdr->msgType == AJ_MSG_METHOD_CALL) { duk_push_c_lightfunc(ctx, AJS_MethodCallError, 1, 0, 0); duk_insert(ctx, -3); (void)duk_pcall_method(ctx, 1); } } } /* * Cleanup stack back to the AJ object */ duk_set_top(ctx, ajIdx + 1); return status; }