void SetValueObject(duk_context* ctx, duk_idx_t stackIndex, void* obj, const char* typeName) { if (stackIndex < 0) --stackIndex; duk_push_pointer(ctx, obj); duk_put_prop_string(ctx, stackIndex, "\xff""obj"); duk_push_pointer(ctx, (void*)typeName); duk_put_prop_string(ctx, stackIndex, "\xff""type"); }
duk_ret_t dukopen_nsp_texture(duk_context *ctx) { duk_idx_t idx = duk_push_object(ctx); duk_idx_t stats_constr = duk_push_c_function(ctx, nsp_texture_constructor, DUK_VARARGS); duk_idx_t stats_prot = duk_push_object(ctx); duk_put_function_list(ctx, stats_prot, nsp_texture_methods); duk_put_prop_string(ctx, stats_constr, "prototype"); duk_put_prop_string(ctx, idx, "Texture"); return 1; }
void StyleContext::parseSceneGlobals(const YAML::Node& node, const std::string& key, int seqIndex, duk_idx_t dukObject) { switch(node.Type()) { case YAML::NodeType::Scalar: if (key.size() == 0) { duk_push_string(m_ctx, node.Scalar().c_str()); duk_put_prop_index(m_ctx, dukObject, seqIndex); } else { auto nodeValue = node.Scalar(); if (nodeValue.compare(0, 8, "function") == 0) { duk_push_string(m_ctx, key.c_str()); // push property key duk_push_string(m_ctx, nodeValue.c_str()); // push function string duk_push_string(m_ctx, ""); if (duk_pcompile(m_ctx, DUK_COMPILE_FUNCTION) == 0) { // compile function duk_put_prop(m_ctx, -3); // put {key: function()} } else { LOGW("Compile failed in global function: %s\n%s\n---", duk_safe_to_string(m_ctx, -1), nodeValue.c_str()); duk_pop(m_ctx); //pop error duk_pop(m_ctx); //pop key // push property as a string duk_push_string(m_ctx, nodeValue.c_str()); duk_put_prop_string(m_ctx, dukObject, key.c_str()); } } else { duk_push_string(m_ctx, nodeValue.c_str()); duk_put_prop_string(m_ctx, dukObject, key.c_str()); } } break; case YAML::NodeType::Sequence: { auto seqObj = duk_push_array(m_ctx); for (int i = 0; i < node.size(); i++) { parseSceneGlobals(node[i], "", i, seqObj); } duk_put_prop_string(m_ctx, seqObj-1, key.c_str()); break; } case YAML::NodeType::Map: { //duk_push_string(m_ctx, key.c_str()); auto mapObj = duk_push_object(m_ctx); for (auto& mapNode : node) { auto itemKey = mapNode.first.Scalar(); parseSceneGlobals(mapNode.second, itemKey, 0, mapObj); } duk_put_prop_string(m_ctx, mapObj-1, key.c_str()); } default: break; } return; }
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; }
void DuktapeContext::set(JNIEnv *env, jstring name, jobject object, jobjectArray methods) { CHECK_STACK(m_context); duk_push_global_object(m_context); const JString instanceName(env, name); if (duk_has_prop_string(m_context, -1, instanceName)) { duk_pop(m_context); queueIllegalArgumentException(env, "A global object called " + instanceName.str() + " already exists"); return; } const duk_idx_t objIndex = duk_require_normalize_index(m_context, duk_push_object(m_context)); // Hook up a finalizer to decrement the refcount and clean up our JavaMethods. duk_push_c_function(m_context, javaObjectFinalizer, 1); duk_set_finalizer(m_context, objIndex); const jsize numMethods = env->GetArrayLength(methods); for (jsize i = 0; i < numMethods; ++i) { jobject method = env->GetObjectArrayElement(methods, i); const jmethodID getName = env->GetMethodID(env->GetObjectClass(method), "getName", "()Ljava/lang/String;"); const JString methodName(env, static_cast<jstring>(env->CallObjectMethod(method, getName))); std::unique_ptr<JavaMethod> javaMethod; try { javaMethod.reset(new JavaMethod(m_javaValues, env, method)); } catch (const std::invalid_argument& e) { queueIllegalArgumentException(env, "In bound method \"" + instanceName.str() + "." + methodName.str() + "\": " + e.what()); // Pop the object being bound and the duktape global object. duk_pop_2(m_context); return; } // Use VARARGS here to allow us to manually validate that the proper number of arguments are // given in the call. If we specify the actual number of arguments needed, Duktape will try to // be helpful by discarding extra or providing missing arguments. That's not quite what we want. // See http://duktape.org/api.html#duk_push_c_function for details. const duk_idx_t func = duk_push_c_function(m_context, javaMethodHandler, DUK_VARARGS); duk_push_pointer(m_context, javaMethod.release()); duk_put_prop_string(m_context, func, JAVA_METHOD_PROP_NAME); // Add this method to the bound object. duk_put_prop_string(m_context, objIndex, methodName); } // Keep a reference in JavaScript to the object being bound. duk_push_pointer(m_context, env->NewGlobalRef(object)); duk_put_prop_string(m_context, objIndex, JAVA_THIS_PROP_NAME); // Make our bound Java object a property of the Duktape global object (so it's a JS global). duk_put_prop_string(m_context, -2, instanceName); // Pop the Duktape global object off the stack. duk_pop(m_context); }
static int sys1_localtime(duk_context *ctx) { struct tm tm, *tmp; time_t timep = duk_to_uint(ctx, 0); tmp = localtime_r(&timep, &tm); if(!tmp) { duk_push_undefined(ctx); return 1; } duk_push_object(ctx); duk_push_uint(ctx, tm.tm_sec); duk_put_prop_string(ctx, 1, "tm_sec"); duk_push_uint(ctx, tm.tm_min); duk_put_prop_string(ctx, 1, "tm_min"); duk_push_uint(ctx, tm.tm_hour); duk_put_prop_string(ctx, 1, "tm_hour"); duk_push_uint(ctx, tm.tm_mday); duk_put_prop_string(ctx, 1, "tm_mday"); duk_push_uint(ctx, tm.tm_mon); duk_put_prop_string(ctx, 1, "tm_mon"); duk_push_uint(ctx, tm.tm_year); duk_put_prop_string(ctx, 1, "tm_year"); duk_push_uint(ctx, tm.tm_wday); duk_put_prop_string(ctx, 1, "tm_wday"); duk_push_uint(ctx, tm.tm_yday); duk_put_prop_string(ctx, 1, "tm_yday"); duk_push_uint(ctx, tm.tm_isdst); duk_put_prop_string(ctx, 1, "tm_isdst"); return 1; }
void duk_mark_jsobject_to_java_object(duk_context *ctx, int index, JNIEnv *env, jobject object){ jobject ref = ((*env)->NewGlobalRef(env, object)); duk_push_pointer(ctx, ref); DEBUG_LOG("ScriptEngine", "duk_push_pointer success %d", index); duk_put_prop_string(ctx, index - 1, JAVA_OBJECT_MARK); duk_push_c_function(ctx, java_object_to_string, 0); duk_put_prop_string(ctx, index - 1, "toString"); duk_push_c_function(ctx, java_object_finalizer, 1); duk_set_finalizer(ctx, index - 1); DEBUG_LOG("ScriptEngine", "new_java_object success"); }
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; }
void JsExe::Register(duk_context* duktapeContext) { duk_push_global_object(duktapeContext); duk_push_c_function(duktapeContext, JsExe::Constructor, DUK_VARARGS); duk_put_prop_string(duktapeContext, -2, "Exe"); duk_push_c_function(duktapeContext, JsExe::Constructor, DUK_VARARGS); duk_put_prop_string(duktapeContext, -2, "Dll"); duk_pop(duktapeContext); }
static duk_ret_t dukzip_unz_getfileinfo(duk_context *ctx) { duk_idx_t info_obj; // unz_file_info fileInfo; unz_file_info64 fileInfo; unzFile archive = dukzip_unz_from_this(ctx); // unzGetCurrentFileInfo(archive, &fileInfo, NULL, 0, NULL, 0, NULL, 0); unzGetCurrentFileInfo64(archive, &fileInfo, NULL, 0, NULL, 0, NULL, 0); #if defined(__GNUC__) && !defined(DUK_NO_VLA) char fileName[fileInfo.size_filename], extraField[fileInfo.size_file_extra], commentString[fileInfo.size_file_comment]; #else char *fileName = malloc(fileInfo.size_filename); char *extraField = malloc(fileInfo.size_file_extra); char *commentString = malloc(fileInfo.size_file_comment); #endif // unzGetCurrentFileInfo(archive, &fileInfo, // fileName, fileInfo.size_filename, // extraField, fileInfo.size_file_extra, // commentString, fileInfo.size_file_comment); unzGetCurrentFileInfo64(archive, &fileInfo, fileName, fileInfo.size_filename, extraField, fileInfo.size_file_extra, commentString, fileInfo.size_file_comment); info_obj = duk_push_object(ctx); duk_push_int(ctx, fileInfo.compressed_size); duk_put_prop_string(ctx, info_obj, "compressed"); duk_push_int(ctx, fileInfo.uncompressed_size); duk_put_prop_string(ctx, info_obj, "uncompressed"); duk_push_lstring(ctx, fileName, fileInfo.size_filename); duk_put_prop_string(ctx, info_obj, "filename"); duk_push_lstring(ctx, extraField, fileInfo.size_file_extra); duk_put_prop_string(ctx, info_obj, "extra"); duk_push_lstring(ctx, commentString, fileInfo.size_file_comment); duk_put_prop_string(ctx, info_obj, "comment"); #if !defined(__GNUC__) || defined(DUK_NO_VLA) free(fileName); free(extraField); free(commentString); #endif return 1; }
static duk_ret_t js_GetClippingRectangle(duk_context* ctx) { rect_t clip; clip = get_clip_rectangle(); duk_push_object(ctx); duk_push_int(ctx, clip.x1); duk_put_prop_string(ctx, -2, "x"); duk_push_int(ctx, clip.y1); duk_put_prop_string(ctx, -2, "y"); duk_push_int(ctx, clip.x2 - clip.x1); duk_put_prop_string(ctx, -2, "width"); duk_push_int(ctx, clip.y2 - clip.y1); duk_put_prop_string(ctx, -2, "height"); return 1; }
void jsapi_init_atomic3d(JSVM* vm) { duk_context* ctx = vm->GetJSContext(); js_class_get_prototype(ctx, "Atomic", "StaticModel"); duk_push_c_function(ctx, StaticModel_SetMaterialIndex, 2); duk_put_prop_string(ctx, -2, "setMaterialIndex"); duk_pop(ctx); // pop AObject prototype js_class_get_prototype(ctx, "Atomic", "CustomGeometry"); duk_push_c_function(ctx, CustomGeometry_SetMaterialIndex, 2); duk_put_prop_string(ctx, -2, "setMaterialIndex"); duk_pop(ctx); // pop AObject prototype }
static void dukzip_push_unzfile(duk_context *ctx, unzFile archive, const char *filename) { /* create object with readable Dukzip archive prototype */ duk_push_object(ctx); duk_get_global_string(ctx, DUKZIP_UNZ_PROTOTYPE); duk_set_prototype(ctx, -2); /* set the archive pointer data */ duk_push_pointer(ctx, archive); duk_put_prop_string(ctx, -2, ZIPHANDLE_PROP); /* set path property */ duk_push_string(ctx, filename); duk_put_prop_string(ctx, -2, ZIPFILENAME_PROP); }
static void register_r2cmd_duktape (RLang *lang, duk_context *ctx) { Gcore = lang->user; duk_push_global_object (ctx); duk_push_c_function (ctx, r2cmd, DUK_VARARGS); duk_put_prop_string (ctx, -2 /*idx:global*/, "r2cmd"); duk_push_c_function (ctx, r2plugin, DUK_VARARGS); duk_put_prop_string (ctx, -2 /*idx:global*/, "r2plugin"); duk_pop (ctx); /* pop global */ // lang_duktape_file (lang, "/tmp/r2.js"); ///usr/share/radare2/0.9.8.git/www/t/r2.js"); lang_duktape_file (lang, PREFIX"/share/radare2/last/www/t/r2.js"); }
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); }
void ncurses_register(duk_context *ctx) { duk_push_global_object(ctx); duk_push_string(ctx, "Ncurses"); duk_push_object(ctx); duk_push_c_function(ctx, ncurses_initscr, 0); duk_put_prop_string(ctx, -2, "initscr"); duk_push_c_function(ctx, ncurses_endwin, 0); duk_put_prop_string(ctx, -2, "endwin"); duk_push_c_function(ctx, ncurses_delscreen, 0); duk_put_prop_string(ctx, -2, "delscreen"); duk_push_c_function(ctx, ncurses_getmaxyx, 0); duk_put_prop_string(ctx, -2, "getmaxyx"); duk_push_c_function(ctx, ncurses_printw, 1); duk_put_prop_string(ctx, -2, "printw"); duk_push_c_function(ctx, ncurses_mvprintw, 3); duk_put_prop_string(ctx, -2, "mvprintw"); duk_push_c_function(ctx, ncurses_refresh, 0); duk_put_prop_string(ctx, -2, "refresh"); duk_push_c_function(ctx, ncurses_getch, 0); duk_put_prop_string(ctx, -2, "getch"); duk_put_prop(ctx, -3); duk_pop(ctx); }
int main(int argc, char *argv[]) { int i, rc; prg.argc = argc; prg.argv = argv; prg.name = argv[1]; duk_context *ctx = duk_create_heap_default(); duk_push_global_object(ctx); duk_push_object(ctx); /* -> [ ... global obj ] */ sys1(ctx); for(i=1;i<argc;i++) { duk_push_string(ctx, argv[i]); duk_put_prop_index(ctx, -2, i-1); } duk_push_number(ctx, argc-1); duk_put_prop_string(ctx, -2, "argc"); duk_put_prop_string(ctx, -2, "Sys1"); /* -> [ ... global ] */ duk_push_object(ctx); /* -> [ ... global obj ] */ prg1(ctx); duk_put_prop_string(ctx, -2, "Prg1"); /* -> [ ... global ] */ prg_push_modsearch(ctx); duk_pop(ctx); prg_parse_appfile(&prg); // push file duk_push_lstring(ctx, prg.main->buf, prg.main->size); duk_push_string(ctx, argv[1]); // execute file (compile + call) prg_register(&prg); rc = duk_safe_call(ctx, prg_wrapped_compile_execute, 2 /*nargs*/, 1 /*nret*/); if (rc != DUK_EXEC_SUCCESS) { print_error(ctx, stderr); exit(2); } duk_pop(ctx); /* pop eval result */ duk_destroy_heap(ctx); return prg.status; }
StyleContext::StyleContext() { m_ctx = duk_create_heap_default(); //// Create global geometry constants // TODO make immutable duk_push_number(m_ctx, GeometryType::points); duk_put_global_string(m_ctx, "point"); duk_push_number(m_ctx, GeometryType::lines); duk_put_global_string(m_ctx, "line"); duk_push_number(m_ctx, GeometryType::polygons); duk_put_global_string(m_ctx, "polygon"); //// Create global 'feature' object // Get Proxy constructor // -> [cons] duk_eval_string(m_ctx, "Proxy"); // Add feature object // -> [cons, { __obj: this }] duk_idx_t featureObj = duk_push_object(m_ctx); duk_push_pointer(m_ctx, this); duk_put_prop_string(m_ctx, featureObj, INSTANCE_ID); // Add handler object // -> [cons, {...}, { get: func, has: func }] duk_idx_t handlerObj = duk_push_object(m_ctx); // Add 'get' property to handler duk_push_c_function(m_ctx, jsGetProperty, 3 /*nargs*/); duk_put_prop_string(m_ctx, handlerObj, "get"); // Add 'has' property to handler duk_push_c_function(m_ctx, jsHasProperty, 2 /*nargs*/); duk_put_prop_string(m_ctx, handlerObj, "has"); // Call proxy constructor // [cons, feature, handler ] -> [obj|error] if (duk_pnew(m_ctx, 2) == 0) { // put feature proxy object in global scope if (!duk_put_global_string(m_ctx, "feature")) { LOGE("Initialization failed"); } } else { LOGE("Failure: %s", duk_safe_to_string(m_ctx, -1)); duk_pop(m_ctx); } DUMP("init\n"); }
duk_ret_t js_register_LocalStorage(duk_context *ctx) { duk_push_global_object(ctx); /* global */ duk_get_prop_string(ctx, -1, MURAL_JS_NAMESPACE); /* global, __MURAL__ */ duk_push_c_function(ctx, w_LocalStorage_constructor, 0); /* global, __MURAL__, constructor */ duk_push_object(ctx); /* global, __MURAL__, constructor, prototype */ duk_put_function_list(ctx, -1, methods_of_LocalStorage); duk_put_prop_string(ctx, -2, "prototype"); /* global, __MURAL__, constructor */ duk_put_prop_string(ctx, -2, "LocalStorage"); /* global, __MURAL__ */ duk_pop_2(ctx); return 0; }
static int sys1_gettimeofday(duk_context *ctx) { pid_t rc; struct timeval tv; rc = gettimeofday(&tv, (void*)0); duk_push_uint(ctx, tv.tv_sec); duk_put_prop_string(ctx, 0, "tv_sec"); duk_push_uint(ctx, tv.tv_usec); duk_put_prop_string(ctx, 0, "tv_usec"); duk_push_int(ctx, rc); return 1; }
duk_ret_t dukky_before_unload_event___proto(duk_context *ctx) { /* Set this prototype's prototype (left-parent) */ /* get prototype */ duk_get_global_string(ctx, dukky_magic_string_prototypes); duk_get_prop_string(ctx, -1, "\xFF\xFFNETSURF_DUKTAPE_PROTOTYPE_EVENT"); duk_replace(ctx, -2); duk_set_prototype(ctx, 0); /* Add read/write property */ duk_dup(ctx, 0); duk_push_string(ctx, "returnValue"); duk_push_c_function(ctx, dukky_before_unload_event_returnValue_getter, 0); duk_push_c_function(ctx, dukky_before_unload_event_returnValue_setter, 1); duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER | DUK_DEFPROP_HAVE_SETTER | DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE | DUK_DEFPROP_HAVE_CONFIGURABLE); duk_pop(ctx); /* Set the destructor */ duk_dup(ctx, 0); duk_push_c_function(ctx, dukky_before_unload_event___destructor, 1); duk_set_finalizer(ctx, -2); duk_pop(ctx); /* Set the constructor */ duk_dup(ctx, 0); duk_push_c_function(ctx, dukky_before_unload_event___constructor, 2); duk_put_prop_string(ctx, -2, "\xFF\xFFNETSURF_DUKTAPE_INIT"); duk_pop(ctx); return 1; /* The prototype object */ }
gpointer _gum_duk_steal_data (duk_context * ctx, duk_idx_t index) { gpointer result = NULL; duk_dup (ctx, index); duk_get_prop_string (ctx, -1, "\xff" "priv"); if (!duk_is_undefined (ctx, -1)) { result = duk_require_pointer (ctx, -1); duk_pop (ctx); duk_push_pointer (ctx, NULL); duk_put_prop_string (ctx, -2, "\xff" "priv"); duk_pop (ctx); } else { duk_pop_2 (ctx); } return result; }
/* * call-seq: * ctx_define_function(name, &block) -> nil * * Define a function defined in the global scope and identified by a name. * * ctx.ctx_define_function("hello_world") { |ctx| 'Hello world' } #=> nil * */ static VALUE ctx_define_function(VALUE self, VALUE prop) { VALUE block; struct state *state; duk_context *ctx; // a block is required if (!rb_block_given_p()) rb_raise(rb_eArgError, "Expected block"); // get the context Data_Get_Struct(self, struct state, state); ctx = state->ctx; // the c function is available in the global scope duk_push_global_object(ctx); duk_push_c_function(ctx, ctx_call_pushed_function, DUK_VARARGS); block = rb_block_proc(); rb_ary_push(state->blocks, block); // block will be properly garbage collected // both block and state are required by the pushed function duk_push_string(ctx, "block"); duk_push_pointer(ctx, (void *) block); duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE | 0); duk_push_string(ctx, "state"); duk_push_pointer(ctx, (void *) state); duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE | 0); duk_put_prop_string(ctx, -2, StringValueCStr(prop)); return Qnil; }
static Box * PrivatePush( duk_context * ctx, const size_t class_index, const size_t object_size, finalizer_t finalizer ) { duk_push_global_object( ctx ); duk_get_prop_string( ctx, -1, "Proxy" ); duk_remove( ctx, -2 ); duk_push_object( ctx ); size_t require_size = sizeof( Box ) + object_size; Box * box = reinterpret_cast<Box*>( duk_push_fixed_buffer( ctx, require_size ) ); box->ClassIndex = class_index; box->Finalizer = finalizer; duk_put_prop_string( ctx, -2, "\xFF" "Box" ); duk_push_c_function( ctx, &internal::ClassFinalizer, 1 ); duk_set_finalizer( ctx, -2 ); duk_push_heap_stash( ctx ); duk_get_prop_string( ctx, -1, "InstanceHandler" ); duk_remove( ctx, -2 ); duk_new( ctx, 2 ); return box; }
int main(int argc, const char *argv[]) { duk_context *ctx = NULL; ctx = duk_create_heap_default(); if (!ctx) { printf("Failed to create a Duktape heap.\n"); exit(1); } duk_push_global_object(ctx); duk_push_c_function(ctx, native_prime_check, 2 /*nargs*/); duk_put_prop_string(ctx, -2, "primeCheckNative"); if (duk_peval_file(ctx, "prime.js") != 0) { printf("Error: %s\n", duk_safe_to_string(ctx, -1)); goto finished; } duk_pop(ctx); /* ignore result */ duk_get_prop_string(ctx, -1, "primeTest"); if (duk_pcall(ctx, 0) != 0) { printf("Error: %s\n", duk_safe_to_string(ctx, -1)); } duk_pop(ctx); /* ignore result */ finished: duk_destroy_heap(ctx); exit(0); }
static duk_ret_t dukky_html_anchor_element_ping_setter(duk_context *ctx) { /* Get private data for method */ html_anchor_element_private_t *priv = NULL; duk_push_this(ctx); duk_get_prop_string(ctx, -1, dukky_magic_string_private); priv = duk_get_pointer(ctx, -1); duk_pop_2(ctx); if (priv == NULL) { return 0; /* can do? No can do. */ } duk_ret_t get_ret; get_ret = dukky_html_anchor_element_ping_getter(ctx); if (get_ret != 1) { return 0; } /* parameter ... attribute */ duk_dup(ctx, 0); /* ... attribute parameter */ /* call the putforward */ duk_put_prop_string(ctx, -2, "value"); return 0; }
void duv_store_handle(duk_context *ctx, void *handle) { duk_push_heap_stash(ctx); duk_dup(ctx, -2); snprintf(key, KEYLEN, "%"PRIXPTR, (uintptr_t)handle); duk_put_prop_string(ctx, -2, key); duk_pop(ctx); }
static void gum_push_module (duk_context * ctx, const GumModuleDetails * details, GumDukCore * core) { duk_push_object (ctx); duk_push_string (ctx, details->name); duk_put_prop_string (ctx, -2, "name"); _gum_duk_push_uint64 (ctx, details->range->base_address, core); duk_put_prop_string (ctx, -2, "base"); duk_push_uint (ctx, details->range->size); duk_put_prop_string (ctx, -2, "size"); }
/** * * performance tips * https://www.ibm.com/developerworks/library/j-jni/ * */ jobject duk_to_java_object(duk_context *ctx, JNIEnv* env, int i){ int type = duk_get_type(ctx, i); if(type == DUK_TYPE_BOOLEAN){ jboolean value = duk_to_boolean(ctx, i); DEBUG_LOG("ScriptEngine","invoke_java_method call, convert %d args to boolean %d", i, value); jmethodID methodID = (*env)->GetMethodID(env, java_boolean_class, "<init>", "(Z)V"); jobject booleanObject = (*env)->NewObject(env, java_boolean_class, methodID, value); return booleanObject; }else if(type == DUK_TYPE_NUMBER){ jdouble value = duk_to_number(ctx, i); jclass doubleClass = (*env)->FindClass(env, "java/lang/Double"); jmethodID methodID = (*env)->GetMethodID(env, doubleClass, "<init>", "(D)V"); jobject numberObject = (*env)->NewObject(env, doubleClass, methodID, value); (*env)->DeleteLocalRef(env, doubleClass); DEBUG_LOG("ScriptEngine","invoke_java_method call, convert %d args to number %f", i, value); return numberObject; }else if(type == DUK_TYPE_STRING){ const char* chs = duk_to_string(ctx, i); DEBUG_LOG("ScriptEngine","invoke_java_method call, convert %d args to string %s", i, chs); return (*env)->NewStringUTF(env, chs); }else if(type == DUK_TYPE_OBJECT){ if(duk_get_prop_string(ctx, i, JAVA_OBJECT_MARK)){ DEBUG_LOG("ScriptEngine","invoke_java_method call, convert %d args to java object", i); jobject value = duk_to_pointer(ctx, -1); duk_pop(ctx); return (*env)->NewLocalRef(env, value); }else{ duk_pop(ctx); if(duk_get_prop_string(ctx, i, JS_REF_MARK)){ DEBUG_LOG("ScriptEngine","reuse javascript object's JSRef"); jweak weakRef = duk_to_pointer(ctx, -1); jobject jsRefObject = (*env)->NewLocalRef(env, weakRef); duk_pop(ctx); if(jsRefObject != NULL){ return jsRefObject; } }else{ duk_pop(ctx); } duk_dup(ctx, i); jint ref = duk_js_ref(ctx); if(ref != 0){ DEBUG_LOG("ScriptEngine","convert javascript object to JSRef Ref Value %d ", ref); jobject engine = get_engine_from_context(ctx); DEBUG_LOG("ScriptEngine","convert javascript object to JSRef Ref Value"); jobject jsRefObject = (*env)->NewObject(env, js_ref_class, js_ref_new_method, engine, ref); jweak jsWeakRef = (*env)->NewWeakGlobalRef(env, jsRefObject); duk_dup(ctx, i); duk_push_pointer(ctx, jsWeakRef); duk_put_prop_string(ctx, -2, JS_REF_MARK); duk_pop(ctx); DEBUG_LOG("ScriptEngine","convert javascript object to JSRef Ref Value Success"); return jsRefObject; } return NULL; } } DEBUG_LOG("ScriptEngine","invoke_java_method call, unhandled type convert %d args to null %s", i, duk_to_string(ctx, i)); return NULL; }
static void gum_push_range (duk_context * ctx, const GumRangeDetails * details, GumDukCore * core) { duk_push_object (ctx); _gum_duk_push_uint64 (ctx, details->range->base_address, core); duk_put_prop_string (ctx, -2, "base"); duk_push_uint (ctx, details->range->size); duk_put_prop_string (ctx, -2, "size"); _gum_duk_push_page_protection (ctx, details->prot); duk_put_prop_string (ctx, -2, "protection"); }