DUK_LOCAL void duk__add_compiler_error_line(duk_hthread *thr) { duk_context *ctx; /* Append a "(line NNN)" to the "message" property of any error * thrown during compilation. Usually compilation errors are * SyntaxErrors but they can also be out-of-memory errors and * the like. */ /* [ ... error ] */ ctx = (duk_context *) thr; DUK_ASSERT(duk_is_object(ctx, -1)); if (!(thr->compile_ctx != NULL && thr->compile_ctx->h_filename != NULL)) { return; } DUK_DDD(DUK_DDDPRINT("compile error, before adding line info: %!T", (duk_tval *) duk_get_tval(ctx, -1))); if (duk_get_prop_stridx(ctx, -1, DUK_STRIDX_MESSAGE)) { duk_push_sprintf(ctx, " (line %ld)", (long) thr->compile_ctx->curr_token.start_line); duk_concat(ctx, 2); duk_put_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE); } else { duk_pop(ctx); } DUK_DDD(DUK_DDDPRINT("compile error, after adding line info: %!T", (duk_tval *) duk_get_tval(ctx, -1))); }
static duk_ret_t dukzip_zip_newfile(duk_context *ctx) { zip_fileinfo zi = {0}; int res = ZIP_OK; zipFile archive = dukzip_zip_from_this(ctx); const char *filename = ""; duk_int_t level = Z_DEFAULT_COMPRESSION; duk_int_t method = Z_DEFLATED; const char *comment = ""; if (duk_is_object(ctx, 0)) { dukzip_zip_checkoptions(ctx, 0, &filename, &level, &method, &comment); } else { filename = duk_require_string(ctx, 0); if (duk_is_number(ctx, 1)) { level = duk_get_int(ctx, 1); } } res = zipOpenNewFileInZip64(archive, filename, &zi, NULL, 0, NULL, 0, comment, method, level, 1); if (res == ZIP_OK) { duk_push_true(ctx); } else { duk_push_false(ctx); } return 1; }
static duk_ret_t dukzip_zip_write(duk_context *ctx) { int res = ZIP_OK; zipFile archive = dukzip_zip_from_this(ctx); if (duk_is_string(ctx, 0)) { int outputl = 0; const char *output = duk_get_lstring(ctx, 0, &outputl); res = zipWriteInFileInZip(archive, output, outputl); } else if (duk_is_buffer(ctx, 0) || duk_is_object(ctx, 0)) { int outputl = 0; void *output = duk_require_buffer_data(ctx, 0, &outputl); res = zipWriteInFileInZip(archive, output, outputl); } else { duk_error(ctx, DUK_ERR_TYPE_ERROR, "unable to write argument to zip file (supported types: string, buffer)"); return -1; } if (res == ZIP_OK) { duk_push_true(ctx); } else { duk_push_false(ctx); } return 1; }
JNIEXPORT void JNICALL Java_com_furture_react_DuktapeEngine_nativeDestory (JNIEnv * env, jobject thisObject, jlong ptr){ duk_context *ctx = convert_to_context(ptr); if(ctx){ jobject enginePtr = get_engine_from_context(ctx); int length = duk_js_ref_size(ctx); for(int i=1; i <= length; i++){ duk_push_js_ref(ctx, i); if(duk_is_object(ctx, -1)){ if(duk_get_prop_string(ctx, -1, JS_REF_MARK)){ jweak weakRef = duk_to_pointer(ctx, -1); if(weakRef != NULL){ (*env)->DeleteWeakGlobalRef(env, weakRef); } DEBUG_LOG("ScriptEngine", "Free jweak Ref onJSRef %d", i); } duk_pop(ctx); } duk_pop(ctx); } duk_destroy_heap(ctx); if(enginePtr != NULL){ (*env)->DeleteGlobalRef(env, enginePtr); } } }
/* * Returns the pid id for a pin object. Also checks that the pin supports the requested function. */ static uint32_t GetPinId(duk_context* ctx, int idx, uint32_t function) { uint32_t id; if (!duk_is_object(ctx, idx)) { duk_error(ctx, DUK_ERR_TYPE_ERROR, "Requires a pin object"); } /* * Get pin id */ duk_get_prop_string(ctx, idx, "id"); id = duk_require_int(ctx, -1); duk_pop(ctx); /* * Check that the required I/O function is supported */ if (function) { const AJS_IO_Info* info = AJS_TargetIO_GetInfo(id); if (!info) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "Undefined I/O pin%d", id); return 0; } if (!(info->functions & function)) { duk_error(ctx, DUK_ERR_TYPE_ERROR, "I/O function %s not supported on pin%d", AJS_IO_FunctionName(function), id); } } return id; }
int duk_builtin_object_constructor(duk_context *ctx) { if (!duk_is_constructor_call(ctx) && !duk_is_null_or_undefined(ctx, 0)) { duk_to_object(ctx, 0); return 1; } if (duk_is_object(ctx, 0)) { return 1; } if (duk_check_type_mask(ctx, 0, DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_BOOLEAN | DUK_TYPE_MASK_NUMBER)) { duk_to_object(ctx, 0); return 1; } /* FIXME: handling for POINTER and BUFFER */ duk_push_object_helper(ctx, DUK_HOBJECT_FLAG_EXTENSIBLE | DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT), DUK_BIDX_OBJECT_PROTOTYPE); return 1; }
/* Some basic tests. */ static duk_ret_t test_1(duk_context *ctx, void *udata) { duk_idx_t thr_idx; duk_context *new_ctx; (void) udata; duk_push_int(ctx, 123); /* dummy */ thr_idx = duk_push_thread(ctx); printf("duk_is_object(%ld) = %d\n", (long) thr_idx, (int) duk_is_object(ctx, thr_idx)); printf("duk_is_thread(%ld) = %d\n", (long) thr_idx, (int) duk_is_thread(ctx, thr_idx)); printf("top=%ld\n", (long) duk_get_top(ctx)); /* use the thread (context) value stack */ new_ctx = duk_get_context(ctx, thr_idx); duk_push_string(new_ctx, "foo"); duk_push_string(new_ctx, "bar"); duk_push_string(new_ctx, "quux"); duk_concat(new_ctx, 3); printf("concat: %s\n", duk_get_string(new_ctx, -1)); /* make new thread unreachable, so it gets GC'd */ duk_set_top(ctx, 0); printf("done\n"); return 0; }
static int UIButton_Popup(duk_context* ctx) { if (!duk_is_object(ctx, 0)) { duk_push_string(ctx, "UIButton.popup first argument must be an object"); duk_throw(ctx); } if (!duk_is_callable(ctx, 1)) { duk_push_string(ctx, "UIButton.popup second argument must be callable"); duk_throw(ctx); } JSVM* vm = JSVM::GetJSVM(ctx); duk_enum(ctx, 0, DUK_ENUM_OWN_PROPERTIES_ONLY); UISelectItemSource* source = new UISelectItemSource(vm->GetContext()); while (duk_next(ctx, -1, 0)) { String key = duk_get_string(ctx, -1); duk_get_prop(ctx, 0); if (duk_is_array(ctx, -1)) { // need to support this, for skin image, etc assert(0); } else if (duk_is_string(ctx, -1)) { // id String id = duk_get_string(ctx, -1); source->AddItem(new UISelectItem(vm->GetContext(), key, id)); } else { duk_push_string(ctx, "UIButton.popup data object key is not an array or string"); duk_throw(ctx); } duk_pop(ctx); // pop key value } duk_pop(ctx); // pop enum object duk_push_this(ctx); duk_dup(ctx, 1); duk_put_prop_string(ctx, -2, "__popup_menu_callback"); UIButton* button = js_to_class_instance<UIButton>(ctx, -1, 0); UIMenuWindow* menuWindow = new UIMenuWindow(vm->GetContext(), button, "__popup-menu"); menuWindow->Show(source); duk_pop(ctx); return 0; }
DUK_INTERNAL duk_ret_t duk_bi_object_constructor(duk_context *ctx) { if (!duk_is_constructor_call(ctx) && !duk_is_null_or_undefined(ctx, 0)) { duk_to_object(ctx, 0); return 1; } if (duk_is_object(ctx, 0)) { return 1; } /* Pointer and buffer primitive values are treated like other * primitives values which have a fully fledged object counterpart: * promote to an object value. Lightfuncs are coerced with * ToObject() even they could also be returned as is. */ if (duk_check_type_mask(ctx, 0, DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_BOOLEAN | DUK_TYPE_MASK_NUMBER | DUK_TYPE_MASK_POINTER | DUK_TYPE_MASK_BUFFER | DUK_TYPE_MASK_LIGHTFUNC)) { duk_to_object(ctx, 0); return 1; } duk_push_object_helper(ctx, DUK_HOBJECT_FLAG_EXTENSIBLE | DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT), DUK_BIDX_OBJECT_PROTOTYPE); return 1; }
void JSEventDispatcher::EndSendEvent(Context* context, Object* sender, StringHash eventType, VariantMap& eventData) { if (!jsEvents_.Contains(eventType)) return; JSVM* vm = JSVM::GetJSVM(NULL); if (!vm) return; duk_context* ctx = vm->GetJSContext(); duk_push_global_stash(ctx); duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_VARIANTMAP_CACHE); duk_push_pointer(ctx, (void*) &eventData); duk_get_prop(ctx, -2); // If this issue is addressed, revisit this to simply remove // the variantmap object from the cache, if the user explicitly // keeps the event object alive in a local closure, that is allowed // (though, will keep object properties from being GC'd) // https://github.com/svaarala/duktape/issues/229 // Ok, this is unfortunate, in an event callback it is possible // to capture the Proxy object which represents the event VariantMap // in a function() {} closure, which will keep the event data alive // until the function happens to be gc'd (it is a member of the eventhandler) // which will leave things like scenes up if there was a P_SCENE event data // member, etc // So, we need to check if we have an object in the variant map cache // and thense call it's delete property method on the Proxy, which will clear // all the data (the proxy can still be alive as a captured local, though // the members won't be held // This all makes it that much more important that the pointer to eventData // is consistent across entire event, otherwise references may be held // see note above about: https://github.com/svaarala/duktape/issues/229 if (duk_is_object(ctx, -1)) { // deletes all properties, thus freeing references, even if // the variant map object is held onto by script (it will be invalid, post // event send) // see JSAPI.cpp variantmap_property_deleteproperty duk_del_prop_index(ctx, -1, 0); duk_push_pointer(ctx, (void*) &eventData); duk_push_undefined(ctx); // clear the variant map object from the cache duk_put_prop(ctx, -4); } duk_pop_3(ctx); }
duk_bool_t duv_is_handle_of(duk_context *ctx, int index, duv_type_mask_t mask) { if (!duk_is_object(ctx, index)) return 0; duk_get_prop_string(ctx, index, "\xff""uv-type"); int type = duk_get_int(ctx, -1); duk_bool_t is = (1 << type) & mask; duk_pop(ctx); return is; }
static int GetStackRaw(duk_context *ctx) { if (!duk_is_object(ctx, -1) || !duk_has_prop_string(ctx, -1, "stack") || !duk_is_error(ctx, -1)) return 1; duk_get_prop_string(ctx, -1, "stack"); duk_remove(ctx, -2); return 1; }
void PushWeakObject(duk_context* ctx, Object* object) { if (!object) { duk_push_null(ctx); return; } duk_push_heap_stash(ctx); // Check if the wrapper for the object already exists in stash // This is required so that comparisons of object references (e.g. against the me property) work properly if (duk_has_prop_index(ctx, -1, (size_t)object)) { duk_get_prop_index(ctx, -1, (size_t)object); WeakPtr<Object>* oldPtr = GetWeakPtr(ctx, -1); if (oldPtr && oldPtr->Get() == object) { duk_remove(ctx, -2); // Remove stash return; } else duk_pop(ctx); // Valid existing wrapper not found } duk_push_object(ctx); WeakPtr<Object>* ptr = new WeakPtr<Object>(object); duk_push_pointer(ctx, ptr); duk_put_prop_string(ctx, -2, "\xff""weak"); duk_push_c_function(ctx, WeakPtr_Finalizer, 1); duk_set_finalizer(ctx, -2); // Set prototype. If not found, use base class prototype (e.g. IComponent) duk_get_global_string(ctx, object->GetTypeName().CString()); if (!duk_is_object(ctx, -1)) { duk_pop(ctx); duk_get_global_string(ctx, object->GetTypeInfo()->GetBaseTypeInfo()->GetTypeName().CString()); } duk_get_prop_string(ctx, -1, "prototype"); duk_set_prototype(ctx, -3); duk_pop(ctx); // Proxied property access handling for scene, entity & component if (object->GetType() == Scene::GetTypeStatic()) SetupProxy(ctx, SceneProxyFunctions); if (object->GetType() == Entity::GetTypeStatic()) SetupProxy(ctx, EntityProxyFunctions); else if (dynamic_cast<IComponent*>(object)) SetupProxy(ctx, ComponentProxyFunctions); // Store to stash duk_dup(ctx, -1); duk_put_prop_index(ctx, -3, (size_t)object); duk_remove(ctx, -2); // Remove stash }
DUK_INTERNAL duk_ret_t duk_bi_function_prototype_apply(duk_context *ctx) { duk_idx_t len; duk_idx_t i; DUK_ASSERT_TOP(ctx, 2); /* not a vararg function */ duk_push_this(ctx); if (!duk_is_callable(ctx, -1)) { DUK_DDD(DUK_DDDPRINT("func is not callable")); goto type_error; } duk_insert(ctx, 0); DUK_ASSERT_TOP(ctx, 3); DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argArray=%!iT", (duk_tval *) duk_get_tval(ctx, 0), (duk_tval *) duk_get_tval(ctx, 1), (duk_tval *) duk_get_tval(ctx, 2))); /* [ func thisArg argArray ] */ if (duk_is_null_or_undefined(ctx, 2)) { DUK_DDD(DUK_DDDPRINT("argArray is null/undefined, no args")); len = 0; } else if (!duk_is_object(ctx, 2)) { goto type_error; } else { DUK_DDD(DUK_DDDPRINT("argArray is an object")); /* XXX: make this an internal helper */ duk_get_prop_stridx(ctx, 2, DUK_STRIDX_LENGTH); len = (duk_idx_t) duk_to_uint32(ctx, -1); /* ToUint32() coercion required */ duk_pop(ctx); duk_require_stack(ctx, len); DUK_DDD(DUK_DDDPRINT("argArray length is %ld", (long) len)); for (i = 0; i < len; i++) { duk_get_prop_index(ctx, 2, i); } } duk_remove(ctx, 2); DUK_ASSERT_TOP(ctx, 2 + len); /* [ func thisArg arg1 ... argN ] */ DUK_DDD(DUK_DDDPRINT("apply, func=%!iT, thisArg=%!iT, len=%ld", (duk_tval *) duk_get_tval(ctx, 0), (duk_tval *) duk_get_tval(ctx, 1), (long) len)); duk_call_method(ctx, len); return 1; type_error: return DUK_RET_TYPE_ERROR; }
void test(duk_context *ctx) { duk_idx_t i, n; duk_push_undefined(ctx); duk_push_null(ctx); duk_push_boolean(ctx, 0); duk_push_boolean(ctx, 123); duk_push_number(ctx, 234); duk_push_string(ctx, "foo"); duk_push_object(ctx); duk_push_array(ctx); duk_push_c_function(ctx, my_c_func, DUK_VARARGS); duk_push_fixed_buffer(ctx, 1024); duk_push_dynamic_buffer(ctx, 1024); duk_push_pointer(ctx, (void *) 0xdeadbeefUL); n = duk_get_top(ctx); for (i = 0; i < n + 1; i++) { /* end on invalid index on purpose */ duk_int_t typeval, typemask; typeval = duk_get_type(ctx, i); typemask = duk_get_type_mask(ctx, i); printf("stack[%ld] --> type=%ld mask=0x%08lx ", (long) i, (long) typeval, (long) typemask); switch(duk_get_type(ctx, i)) { case DUK_TYPE_NONE: printf("none"); break; case DUK_TYPE_UNDEFINED: printf("undefined"); break; case DUK_TYPE_NULL: printf("null"); break; case DUK_TYPE_BOOLEAN: printf("boolean"); break; case DUK_TYPE_NUMBER: printf("number"); break; case DUK_TYPE_STRING: printf("string"); break; case DUK_TYPE_OBJECT: printf("object"); break; case DUK_TYPE_BUFFER: printf("buffer"); break; case DUK_TYPE_POINTER: printf("pointer"); break; default: printf("unknown(%d)", (int) duk_get_type(ctx, i)); break; } printf(" bool=%d num=%lf str=%s buf-is-null=%d ptr=%p", (int) duk_get_boolean(ctx, i), (double) duk_get_number(ctx, i), duk_get_string(ctx, i), (duk_get_buffer(ctx, i, NULL) == NULL ? 1 : 0), duk_get_pointer(ctx, i)); printf(" isobj=%d isarr=%d isfunc=%d", (int) duk_is_object(ctx, i), (int) duk_is_array(ctx, i), (int) duk_is_function(ctx, i)); printf("\n"); } }
gboolean _gum_duk_get_pointer (duk_context * ctx, duk_idx_t index, GumDukCore * core, gpointer * ptr) { gboolean success = TRUE; duk_dup (ctx, index); duk_push_heapptr (ctx, core->native_pointer); if (duk_is_pointer (ctx, -2)) { *ptr = duk_require_pointer (ctx, -2); } else if (duk_instanceof (ctx, -2, -1)) { GumDukNativePointer * p; p = _gum_duk_require_data (ctx, -2); *ptr = p->value; } else if (duk_is_object (ctx, -2)) { gboolean is_native_pointer; duk_get_prop_string (ctx, -2, "handle"); is_native_pointer = duk_instanceof (ctx, -1, -2); if (is_native_pointer) { GumDukNativePointer * p; p = _gum_duk_require_data (ctx, -1); *ptr = p->value; } else { success = FALSE; } duk_pop (ctx); } else { success = FALSE; } duk_pop_2 (ctx); return success; }
/* * Looks for an interface that defines a specific member. * * TODO - check for ambiguity - same member defined on multiple interfaces */ static const char* FindInterfaceForMember(duk_context* ctx, duk_idx_t mbrIdx, const char** member) { const char* iface = NULL; uint8_t found = FALSE; size_t numInterfaces; duk_idx_t listIdx; duk_get_prop_string(ctx, -1, "interfaces"); numInterfaces = duk_get_length(ctx, -1); listIdx = AJS_GetAllJoynProperty(ctx, "interfaceDefinition"); if (duk_is_object(ctx, mbrIdx)) { /* * Expect an object of form { member:"org.foo.interface" } */ duk_enum(ctx, mbrIdx, DUK_ENUM_OWN_PROPERTIES_ONLY); if (!duk_next(ctx, -1, 1)) { duk_error(ctx, DUK_ERR_TYPE_ERROR, "Require object of form { 'member-name':'interface-name' }"); } iface = duk_require_string(ctx, -1); if (!AJ_StringFindFirstOf(iface, ".") == -1) { duk_error(ctx, DUK_ERR_TYPE_ERROR, "Interface name '%s' is not a dotted name", iface); } *member = duk_require_string(ctx, -2); duk_get_prop_string(ctx, listIdx, iface); if (duk_is_undefined(ctx, -1)) { duk_error(ctx, DUK_ERR_REFERENCE_ERROR, "Unknown interface: '%s'", iface); } found = duk_has_prop_string(ctx, -1, *member); duk_pop_n(ctx, 4); } else { size_t i; /* * Expect a string */ *member = duk_require_string(ctx, mbrIdx); for (i = 0; !found && (i < numInterfaces); ++i) { duk_get_prop_index(ctx, -2, i); iface = duk_require_string(ctx, -1); duk_get_prop_string(ctx, listIdx, iface); /* * See if the requested member exists on this interface */ found = duk_has_prop_string(ctx, -1, *member); duk_pop_2(ctx); } } duk_pop_2(ctx); if (!found) { duk_error(ctx, DUK_ERR_REFERENCE_ERROR, "Unknown member: '%s'", *member); } return iface; }
int duk_bi_error_prototype_to_string(duk_context *ctx) { /* FIXME: optimize with more direct internal access */ duk_push_this(ctx); if (!duk_is_object(ctx, -1)) { goto type_error; } /* [ ... this ] */ duk_get_prop_stridx(ctx, -1, DUK_STRIDX_NAME); if (duk_is_undefined(ctx, -1)) { duk_pop(ctx); duk_push_string(ctx, "Error"); } else { duk_to_string(ctx, -1); } /* [ ... this name ] */ /* FIXME: Are steps 6 and 7 in E5 Section 15.11.4.4 duplicated by * accident or are they actually needed? The first ToString() * could conceivably return 'undefined'. */ duk_get_prop_stridx(ctx, -2, DUK_STRIDX_MESSAGE); if (duk_is_undefined(ctx, -1)) { duk_pop(ctx); duk_push_string(ctx, ""); } else { duk_to_string(ctx, -1); } /* [ ... this name message ] */ if (duk_get_length(ctx, -2) == 0) { /* name is empty -> return message */ return 1; } if (duk_get_length(ctx, -1) == 0) { /* message is empty -> return name */ duk_pop(ctx); return 1; } duk_push_string(ctx, ": "); duk_insert(ctx, -2); /* ... name ': ' message */ duk_concat(ctx, 3); return 1; type_error: return DUK_RET_TYPE_ERROR; }
WeakPtr<Object>* GetWeakPtr(duk_context* ctx, duk_idx_t stackIndex) { if (!duk_is_object(ctx, stackIndex)) return nullptr; WeakPtr<Object>* ptr = nullptr; duk_get_prop_string(ctx, stackIndex, "\xff""weak"); if (duk_is_pointer(ctx, -1)) ptr = static_cast<WeakPtr<Object>*>(duk_to_pointer(ctx, -1)); duk_pop(ctx); return ptr; }
static int get_stack_raw(duk_context *ctx) { if (!duk_is_object(ctx, -1)) { return 1; } if (!duk_has_prop_string(ctx, -1, "stack")) { return 1; } /* XXX: should check here that object is an Error instance too, * i.e. 'stack' is special. */ duk_get_prop_string(ctx, -1, "stack"); /* caller coerces */ duk_remove(ctx, -2); return 1; }
static int get_stack_raw(duk_context *ctx) { if (!duk_is_object(ctx, -1)) { return 1; } if (!duk_has_prop_string(ctx, -1, "stack")) { return 1; } if (!duk_is_error(ctx, -1)) { /* Not an Error instance, don't read "stack". */ return 1; } duk_get_prop_string(ctx, -1, "stack"); /* caller coerces */ duk_remove(ctx, -2); return 1; }
static duk_ret_t my_func(duk_context *ctx) { duk_c_function funcptr; printf("my_func, top=%ld\n", (long) duk_get_top(ctx)); duk_push_current_function(ctx); printf("type=%d\n", (int) duk_get_type(ctx, -1)); printf("duk_is_object: %d\n", (int) duk_is_object(ctx, -1)); printf("duk_is_function: %d\n", (int) duk_is_function(ctx, -1)); funcptr = duk_get_c_function(ctx, -1); printf("duk_get_c_function matches my_func: %d\n", (my_func == funcptr ? 1 : 0)); printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
AJ_Status AJS_HandleJoinSessionReply(duk_context* ctx, AJ_Message* msg) { const char* peer = NULL; SessionInfo* sessionInfo = NULL; uint32_t replySerial = msg->replySerial; uint8_t joined = FALSE; AJS_GetGlobalStashObject(ctx, "sessions"); duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY); while (duk_next(ctx, -1, 1)) { peer = duk_get_string(ctx, -2); AJ_ASSERT(duk_is_object(ctx, -1)); duk_get_prop_string(ctx, -1, "info"); sessionInfo = duk_get_buffer(ctx, -1, NULL); AJ_ASSERT(sessionInfo); duk_pop_3(ctx); if (sessionInfo->replySerial == replySerial) { uint32_t sessionId; uint32_t replyStatus; /* * Check if the join was successful */ AJ_UnmarshalArgs(msg, "uu", &replyStatus, &sessionId); if (replyStatus == AJ_JOINSESSION_REPLY_SUCCESS) { /* * TODO - if we have a well-known name send a ping to get the unique name */ sessionInfo->sessionId = sessionId; joined = TRUE; } sessionInfo->replySerial = 0; break; } } duk_pop(ctx); /* Pop the enum */ if (joined) { /* * TODO - we may need to initiate authentication with the remote peer */ AnnouncementCallbacks(ctx, peer, sessionInfo); } /* Pop "sessions" */ duk_pop(ctx); return AJ_OK; }
Vector<String> GetStringVector(duk_context* ctx, duk_idx_t stackIndex) { Vector<String> ret; if (duk_is_object(ctx, stackIndex)) { duk_size_t len = duk_get_length(ctx, stackIndex); for (duk_size_t i = 0; i < len; ++i) { duk_get_prop_index(ctx, stackIndex, i); if (duk_is_string(ctx, -1)) ret.Push(String(duk_get_string(ctx, -1))); duk_pop(ctx); } } return ret; }
AJ_Status AJS_ServiceIO(duk_context* ctx) { AJS_IO_PinTriggerCondition condition; int32_t trigId; trigId = AJS_TargetIO_PinTrigId(&condition); if (trigId != AJS_IO_PIN_NO_TRIGGER) { AJ_InfoPrintf(("triggered on id %d\n", trigId)); /* * Lookup the pin object in the triggers array */ duk_get_global_string(ctx, AJS_IOObjectName); duk_get_prop_string(ctx, -1, AJS_HIDDEN_PROP("trigs")); do { duk_get_prop_index(ctx, -1, trigId); if (duk_is_object(ctx, -1)) { /* * Call the trigger function passing the pin object and value as the argument */ duk_get_prop_string(ctx, -1, "trigger"); if (duk_is_callable(ctx, -1)) { /* * Pin object is the "this" object */ duk_dup(ctx, -2); duk_push_int(ctx, condition); if (duk_pcall_method(ctx, 1) != DUK_EXEC_SUCCESS) { AJS_ConsoleSignalError(ctx); } } /* * Pop pin object */ duk_pop(ctx); } else { AJ_ErrPrintf(("Expected a pin object trigId = %d\n", trigId)); } duk_pop(ctx); trigId = AJS_TargetIO_PinTrigId(&condition); } while (trigId != AJS_IO_PIN_NO_TRIGGER); duk_pop_2(ctx); } return AJ_OK; }
void test(duk_context *ctx) { duk_idx_t obj_idx; duk_push_int(ctx, 123); /* dummy */ obj_idx = duk_push_object(ctx); duk_push_int(ctx, 42); duk_put_prop_string(ctx, obj_idx, "meaningOfLife"); /* object is now: { "meaningOfLife": 42 } */ printf("duk_is_object(%ld) = %d\n", (long) obj_idx, (int) duk_is_object(ctx, obj_idx)); duk_json_encode(ctx, obj_idx); /* in-place */ printf("json encoded: %s\n", duk_get_string(ctx, obj_idx)); printf("top=%ld\n", (long) duk_get_top(ctx)); }
static void print_error(duk_context *ctx, FILE *f) { if (duk_is_object(ctx, -1) && duk_has_prop_string(ctx, -1, "stack")) { /* FIXME: print error objects specially */ /* FIXME: pcall the string coercion */ duk_get_prop_string (ctx, -1, "stack"); if (duk_is_string (ctx, -1)) { fprintf (f, "%s\n", duk_get_string(ctx, -1)); fflush (f); duk_pop_2 (ctx); return; } else { duk_pop (ctx); } } duk_to_string(ctx, -1); fprintf (f, "%s\n", duk_get_string(ctx, -1)); fflush (f); duk_pop(ctx); }
void test(duk_context *ctx) { duk_ret_t rc; /* first test what happens when there is no running function */ printf("no running function\n"); duk_push_current_function(ctx); printf("type=%d\n", (int) duk_get_type(ctx, -1)); printf("duk_is_object: %d\n", (int) duk_is_object(ctx, -1)); printf("duk_is_function: %d\n", (int) duk_is_function(ctx, -1)); duk_pop(ctx); /* then test the basic case */ printf("basic case\n"); duk_push_c_function(ctx, my_func, 1 /*nargs*/); duk_push_int(ctx, 123); rc = duk_pcall(ctx, 1); printf("rc=%d, result='%s'\n", (int) rc, duk_to_string(ctx, -1)); duk_pop(ctx); }
GumCpuContext * _gum_duk_get_cpu_context (duk_context * ctx, duk_idx_t index, GumDukCore * core) { gboolean is_cpu_context; GumDukCpuContext * instance; if (!duk_is_object (ctx, index)) return NULL; duk_dup (ctx, index); duk_push_heapptr (ctx, core->cpu_context); is_cpu_context = duk_instanceof (ctx, -2, -1); duk_pop_2 (ctx); if (!is_cpu_context) return NULL; instance = _gum_duk_require_data (ctx, index); return instance->handle; }
CEJsValue js_to_cejs_value(duk_context* ctx, int index) { if (duk_is_number(ctx, index)) return JS_NUMBER(duk_to_number(ctx, index)); if (duk_is_boolean(ctx, index)) return JS_BOOL(duk_to_boolean(ctx, index)); if (duk_is_string(ctx, index)) return JS_STRING(duk_to_string(ctx, index)); if (duk_is_array(ctx, index)) return JS_STRING(duk_json_encode(ctx, index)); if (duk_is_object(ctx, index)) return JS_STRING(duk_json_encode(ctx, index)); if (duk_is_undefined(ctx, index)) { return JS_UNDEFINED; } return JS_NULL; }