/* * Evaluate a string as Duktape code in the selected heap. * Usage: eval token code * Return value: the result of the evaluation coerced to string. * Side effects: may change the Duktape interpreter heap. */ static int Eval_Cmd(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { duk_context *ctx; duk_int_t duk_result; const char *js_code; if (objc != 3) { Tcl_WrongNumArgs(interp, 1, objv, USAGE_EVAL); return TCL_ERROR; } ctx = parse_id(cdata, interp, objv[1], 0); if (ctx == NULL) { return TCL_ERROR; } js_code = Tcl_GetString(objv[2]); duk_result = duk_peval_string(ctx, js_code); Tcl_SetObjResult(interp, Tcl_NewStringObj( duk_safe_to_string(ctx, -1), -1)); duk_pop(ctx); if (duk_result == 0) { return TCL_OK; } else { return TCL_ERROR; } }
void test_eval(duk_context* ctx, const char* code) { if (duk_peval_string(ctx, code) != 0) { duk_get_prop_string(ctx, -1, "stack"); std::cerr << "Error running '" << code << "':" << std::endl; std::cerr << duk_safe_to_string(ctx, -1) << std::endl; duk_pop(ctx); test_assert(false); } }
JNIEXPORT jobject JNICALL Java_com_furture_react_DuktapeEngine_nativeExeclute (JNIEnv *env, jobject thisObject, jlong ptr, jstring script){ jboolean iscopy = JNI_FALSE; const char* src = ((*env)->GetStringUTFChars(env, script, &iscopy)); duk_context *ctx = convert_to_context(ptr); if(duk_peval_string(ctx, src) != DUK_EXEC_SUCCESS){ LOGE("ScriptEngine", "ScriptEngine eval_string %s\n", duk_js_error_to_string(ctx, -1)); duk_push_null(ctx); } if(src != NULL){ (*env)->ReleaseStringUTFChars(env, script, src); } return duk_to_java_object(ctx, env, -1); }
static bool initJavascript(tic_mem* tic, const char* code) { tic_machine* machine = (tic_machine*)tic; initDuktape(machine); duk_context* duktape = machine->js; if (duk_pcompile_string(duktape, 0, code) != 0 || duk_peval_string(duktape, code) != 0) { machine->data->error(machine->data->data, duk_safe_to_string(duktape, -1)); duk_pop(duktape); return false; } return true; }
int main(int argc, char *argv[]) { duk_context *ctx; int i; int exitcode = 0; ctx = duk_create_heap_default(); if (!ctx) { return 1; } duk_push_c_function(ctx, handle_print, 1); duk_put_global_string(ctx, "print"); duk_push_c_function(ctx, handle_assert, 2); duk_put_global_string(ctx, "assert"); duk_push_object(ctx); duk_push_c_function(ctx, cb_resolve_module, DUK_VARARGS); duk_put_prop_string(ctx, -2, "resolve"); duk_push_c_function(ctx, cb_load_module, DUK_VARARGS); duk_put_prop_string(ctx, -2, "load"); duk_module_node_init(ctx); printf("top after init: %ld\n", (long) duk_get_top(ctx)); for (i = 1; i < argc; i++) { printf("Evaling: %s\n", argv[i]); if (duk_peval_string(ctx, argv[i]) != 0) { if (duk_get_prop_string(ctx, -1, "stack")) { duk_replace(ctx, -2); } else { duk_pop(ctx); } exitcode = 1; } printf("--> %s\n", duk_safe_to_string(ctx, -1)); duk_pop(ctx); } printf("Done\n"); duk_destroy_heap(ctx); return exitcode; }
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; }
/** Run the accessor. * @param accessorFileName The file name of the accessor, suitable getAccessorCode() * @param timeout The number of milliseconds to wait after the * accessor is instantiated and initialized. If the timeout is less than zero, * then the timeout will be forever. * @return 0 if successfully, non-zero if there is an error. */ int runAccessorHost(duk_context *ctx, const char *accessorFileName, int timeout) { int rc; //fprintf(stderr, "%s:%d: About to load C version of c_eventloop.\n", __FILE__, __LINE__); // Use duk_peval_string_noresult() and avoid interning the string. Good // for low memory, see // http://duktape.org/api.html#duk_peval_string_noresult if (duk_peval_string(ctx, c_eventloop_js) != 0) { fprintf(stderr, "%s:%d: Loading C version of c_eventloop failed. Error was:\n", __FILE__, __LINE__); print_pop_error(ctx, stderr); return 1; } else { //printf("%s: Loading C version of c_eventloop worked\n", __FILE__); duk_pop(ctx); } // Use duk_peval_string_noresult() and avoid interning the string. Good // for low memory, see // http://duktape.org/api.html#duk_peval_string_noresult if (duk_peval_string(ctx, ___duktapeHost_js) != 0) { fprintf(stderr, "%s:%d: Loading C version of duktapeHost failed. Error was:\n", __FILE__, __LINE__); print_pop_error(ctx, stderr); return 2; } else { //printf("%s: Loading C version of duktapeHost worked\n", __FILE__); duk_pop(ctx); } // Call instantiateAndInitialize() on the accessorFileName, then timeout. // Build the command to be evaled. // FIXME: Note that for deployment, we could save memory and // choose one or the other at compile time. int length = strlen(accessorFileName); if (timeout >= 0) { // Increase 136 if the first snprintf string changes length += 136 + 8 /* Approximate Length of timeout value as a string */; } else { // Increase 79 if the second snprintf string changes. length += 79; } char buf[length]; if (timeout >= 0) { // Timeout. // While exiting, invoke wrapup() on any accessors that were // created. The TrainableTest accessor expects wrapup to be // called so that it can report an error if fire() was never // called. // requestEventLoopExit() is defined in ecma_eventloop.js snprintf(buf, length, "var a=['%s'],t=this;t.b=instantiateAndInitialize(a),setTimeout(function(){for(var i in t.b)t.b[i].wrapup();requestEventLoopExit()},%d);", accessorFileName, timeout); } else { // Prevent the script from exiting by repeating the empty function // every ~25 days. snprintf(buf, length, "var a=['%s'];instantiateAndInitialize(a);setInterval(function(){},2147483647);", accessorFileName); } // Eval the command, avoid interning the string. if (duk_peval_string(ctx, buf) != 0) { fprintf(stderr, "%s:%d: Failed to invoke accessor %s. Command was:\n%s\nError was:\n", __FILE__, __LINE__, accessorFileName, buf); print_pop_error(ctx, stderr); return 3; } else { duk_pop(ctx); } // Compile solution //duk_compile(ctx, 0); /* duk_push_global_object(ctx); /\* 'this' binding *\/ */ /* duk_insert(ctx, -2); /\* [ ... global func ] *\/ */ /* duk_put_prop_string(ctx, -2, "_USERCODE"); */ /* duk_pop(ctx); */ /* duk_eval_string(ctx, "setTimeout(function() { _USERCODE(); }, 0);"); */ /* duk_pop(ctx); */ rc = duk_safe_call(ctx, eventloop_run, 0 /*nargs*/, 1 /*nrets*/); if (rc != 0) { fprintf(stderr, "%s:%d: %s: Failed invoke eventloop_run()\n", __FILE__, __LINE__, accessorFileName); return 4; } //fprintf(stderr, "runAccessorHost() done.\n"); return 0; }
//duk_int_t duk_peval_string(duk_context *ctx, const char *src); duk_int_t aperl_duk_peval_string(duk_context *ctx, const char *src) { duk_int_t ret = duk_peval_string(ctx, src); return ret; }
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; }
/* * Call a JS method/function. * Usage: call token method this ?{arg ?type?}? ... * Return value: the result of the method call coerced to string. * Side effects: may change the Duktape interpreter heap. */ static int CallMethod_Cmd(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { int i; int list_length; int tableIndex; int int_value; double double_value; duk_context *ctx; duk_int_t duk_result; Tcl_Obj *value; Tcl_Obj *type; static const char *types[] = { "boolean", "nan", "null", "number", "string", "undefined", (char *)NULL }; enum types { TYPE_BOOLEAN, TYPE_NAN, TYPE_NULL, TYPE_NUMBER, TYPE_STRING, TYPE_UNDEFINED }; if (objc < 4) { Tcl_WrongNumArgs(interp, 1, objv, USAGE_CALL_METHOD); return TCL_ERROR; } ctx = parse_id(cdata, interp, objv[1], 0); if (ctx == NULL) { return TCL_ERROR; } /* Eval the function name and "this" to put them on the stack. */ for (i = 2; i < 4; i++) { duk_result = duk_peval_string(ctx, Tcl_GetString(objv[i])); if (duk_result != 0) { Tcl_SetObjResult(interp, Tcl_NewStringObj( duk_safe_to_string(ctx, -1), -1)); duk_pop(ctx); return TCL_ERROR; } } /* Push the arguments. */ for (i = 4; i < objc; i++) { if (Tcl_ListObjIndex(interp, objv[i], 0, &value) != TCL_OK) { return TCL_ERROR; } if (Tcl_ListObjLength(interp, objv[i], &list_length) != TCL_OK) { return TCL_ERROR; } if (list_length == 2) { if (Tcl_ListObjIndex(interp, objv[i], 1, &type) != TCL_OK) { return TCL_ERROR; } if (Tcl_GetIndexFromObj(interp, type, types, "type", 0, &tableIndex) != TCL_OK) { return TCL_ERROR; } } else if (list_length == 1) { tableIndex = TYPE_STRING; } else { Tcl_SetObjResult(interp, Tcl_NewStringObj(ERROR_ARG_LENGTH, -1)); return TCL_ERROR; } switch ((enum types) tableIndex) { case TYPE_BOOLEAN: if (Tcl_GetIntFromObj(interp, value, &int_value) != TCL_OK) { return TCL_ERROR; } duk_push_boolean(ctx, int_value); break; case TYPE_NAN: duk_push_nan(ctx); break; case TYPE_NULL: duk_push_null(ctx); break; case TYPE_NUMBER: if (Tcl_GetDoubleFromObj(interp, value, &double_value) != TCL_OK) { return TCL_ERROR; } duk_push_number(ctx, double_value); break; case TYPE_UNDEFINED: duk_push_undefined(ctx); break; case TYPE_STRING: default: duk_push_string(ctx, Tcl_GetString(value)); break; } } duk_result = duk_pcall_method(ctx, objc - 4); Tcl_SetObjResult(interp, Tcl_NewStringObj(duk_safe_to_string(ctx, -1), -1)); duk_pop(ctx); if (duk_result == 0) { return TCL_OK; } else { return TCL_ERROR; } }
void test_eval_expect_error(duk_context* ctx, const char* code) { if (duk_peval_string(ctx, code) == 0) { std::cerr << "Expected error running '" << code << "', but ran fine" << std::endl; test_assert(false); } }