KKJSMethod::KKJSMethod(JSContextRef context, JSObjectRef jsobject, JSObjectRef thisObject) : context(NULL), jsobject(jsobject), thisObject(thisObject) { /* KJS methods run in the global context that they originated from * this seems to prevent nasty crashes from trying to access invalid * contexts later. Global contexts need to be registered by all modules * that use a KJS context. */ JSObjectRef globalObject = JSContextGetGlobalObject(context); JSGlobalContextRef globalContext = KJSUtil::GetGlobalContext(globalObject); // This context hasn't been registered. Something has gone pretty // terribly wrong and Kroll will likely crash soon. Nonetheless, keep // the user up-to-date to keep their hopes up. if (globalContext == NULL) std::cerr << "Could not locate global context for a KJS method." << " One of the modules is misbehaving." << std::endl; this->context = globalContext; KJSUtil::ProtectGlobalContext(this->context); JSValueProtect(this->context, jsobject); if (thisObject != NULL) JSValueProtect(this->context, thisObject); this->kobject = new KKJSObject(this->context, jsobject); }
EJ_BIND_FUNCTION(EJBindingCanvas,createImageData, ctx, argc, argv) { if( argc < 2 ) { return NULL; } float sw = JSValueToNumberFast(ctx, argv[0]), sh = JSValueToNumberFast(ctx, argv[1]); GLubyte * pixels = (GLubyte *)calloc( sw * sh * 4, sizeof(GLubyte) ); EJImageData * imageData = new EJImageData(sw ,sh ,pixels); imageData->autorelease(); // Create the JS object EJBindingImageData* tempData = new EJBindingImageData(); JSClassRef imageDataClass = EJApp::instance()->getJSClassForClass((EJBindingBase*)tempData); delete tempData; JSObjectRef obj = JSObjectMake( ctx, imageDataClass, NULL ); JSValueProtect(ctx, obj); // Create the native instance EJBindingImageData * jsImageData =new EJBindingImageData(ctx,obj,imageData); // Attach the native instance to the js object JSObjectSetPrivate( obj, (void *)jsImageData ); JSValueUnprotect(ctx, obj); return obj; }
void InspectorController::scriptObjectReady() { ASSERT(m_scriptContext); if (!m_scriptContext) return; JSObjectRef global = JSContextGetGlobalObject(m_scriptContext); ASSERT(global); JSStringRef inspectorString = JSStringCreateWithUTF8CString("WebInspector"); JSValueRef inspectorValue = JSObjectGetProperty(m_scriptContext, global, inspectorString, 0); JSStringRelease(inspectorString); ASSERT(inspectorValue); if (!inspectorValue) return; m_scriptObject = JSValueToObject(m_scriptContext, inspectorValue, 0); ASSERT(m_scriptObject); JSValueProtect(m_scriptContext, m_scriptObject); // Make sure our window is visible now that the page loaded m_client->showWindow(); }
EJ_BIND_FUNCTION(EJBindingCanvas,getImageData, ctx, argc, argv) { if( argc < 4 ) { return NULL; } float sx = JSValueToNumberFast(ctx, argv[0]), sy = JSValueToNumberFast(ctx, argv[1]), sw = JSValueToNumberFast(ctx, argv[2]), sh = JSValueToNumberFast(ctx, argv[3]); // Get the image data //ejectaInstance->currentRenderingContext = renderingContext; ejectaInstance->setCurrentRenderingContext(renderingContext); EJImageData * imageData = renderingContext->getImageData(sx,sy,sw,sh); // Create the JS object EJBindingImageData* tempData = new EJBindingImageData(); JSClassRef imageDataClass = EJApp::instance()->getJSClassForClass((EJBindingBase*)tempData); tempData->autorelease(); JSObjectRef obj = JSObjectMake( ctx, imageDataClass, NULL ); JSValueProtect(ctx, obj); // Create the native instance EJBindingImageData * jsImageData = new EJBindingImageData(ctx,obj,imageData); // Attach the native instance to the js object JSObjectSetPrivate( obj, (void *)jsImageData ); JSValueUnprotect(ctx, obj); return obj; }
static JSValueRef nativeFlushQueueImmediate( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { if (argumentCount != 1) { *exception = createErrorString(ctx, "Got wrong number of args"); return JSValueMakeUndefined(ctx); } JSCExecutor *executor; try { executor = s_globalContextRefToJSCExecutor.at(JSContextGetGlobalContext(ctx)); } catch (std::out_of_range& e) { *exception = createErrorString(ctx, "Global JS context didn't map to a valid executor"); return JSValueMakeUndefined(ctx); } JSValueProtect(ctx, arguments[0]); std::string resStr = Value(ctx, arguments[0]).toJSONString(); executor->flushQueueImmediate(resStr); return JSValueMakeUndefined(ctx); }
JSObjectRef make_object_for_class(JSContextRef ctx, int iclass, GObject *o, gboolean protect) { ScriptContext *sctx = scripts_get_context(); if (sctx == NULL) return JSValueToObject(ctx, NIL, NULL); JSObjectRef retobj = g_object_get_qdata(o, sctx->ref_quark); if (retobj != NULL) { goto finish; } retobj = JSObjectMake(ctx, sctx->classes[iclass], o); if (protect) { g_object_set_qdata_full(o, sctx->ref_quark, retobj, (GDestroyNotify)object_destroy_cb); JSValueProtect(ctx, retobj); } else g_object_set_qdata_full(o, sctx->ref_quark, retobj, NULL); finish: scripts_release_context(); return retobj; }
void addAccessibilityNotificationHandler(AccessibilityNotificationHandler* notificationHandler) { if (!notificationHandler) return; #if PLATFORM(GTK) JSGlobalContextRef jsContext = webkit_web_frame_get_global_context(mainFrame); #elif PLATFORM(EFL) JSGlobalContextRef jsContext = DumpRenderTreeSupportEfl::globalContextRefForFrame(browser->mainFrame()); #else JSContextRef jsContext = 0; #endif if (!jsContext) return; JSValueProtect(jsContext, notificationHandler->notificationFunctionCallback()); // Check if this notification handler is related to a specific element. if (notificationHandler->platformElement()) { NotificationHandlersMap::iterator currentNotificationHandler = notificationHandlers.find(notificationHandler->platformElement()); if (currentNotificationHandler != notificationHandlers.end()) { ASSERT(currentNotificationHandler->value->platformElement()); JSValueUnprotect(jsContext, currentNotificationHandler->value->notificationFunctionCallback()); notificationHandlers.remove(currentNotificationHandler->value->platformElement()); } notificationHandlers.add(notificationHandler->platformElement(), notificationHandler); } else { if (globalNotificationHandler) JSValueUnprotect(jsContext, globalNotificationHandler->notificationFunctionCallback()); globalNotificationHandler = notificationHandler; } connectAccessibilityCallbacks(); }
SIGNAL_CALLBACK_ID add_signal_callback(JSContextRef ctx, struct DBusObjectInfo *info, struct Signal *sig, JSObjectRef func) { g_assert(sig != NULL); g_assert(func != NULL); if (__sig_info_hash == NULL) { __sig_info_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)g_hash_table_destroy); } char* key = g_strdup_printf("%s%s%s", info->path, info->iface, sig->name); GHashTable *cbs = g_hash_table_lookup(__sig_info_hash, key); if (cbs == NULL) { cbs = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_info_free); g_hash_table_insert(__sig_info_hash, key, cbs); } struct SignalInfo* sig_info = g_new0(struct SignalInfo, 1); sig_info->name = sig->name; sig_info->signatures = sig->signature; sig_info->path = info->path; sig_info->iface = info->iface; sig_info->callback = func; JSValueProtect(ctx, func); SIGNAL_CALLBACK_ID id = (SIGNAL_CALLBACK_ID)GPOINTER_TO_INT(func); g_hash_table_insert(cbs, GINT_TO_POINTER((int)id), sig_info); return id; }
int add_signal_callback(JSContextRef ctx, struct DBusObjectInfo *info, struct Signal *sig, JSObjectRef func) { g_assert(sig != NULL); g_assert(func != NULL); if (__sig_info_hash == NULL) { __sig_info_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)signal_info_free); } char* key = g_strdup_printf("%s%s%s", info->path, info->iface, sig->name); GSList *infos = g_hash_table_lookup(__sig_info_hash, key); if (infos != NULL) { return -1; //alerady has this callback } struct SignalInfo* sig_info = g_new0(struct SignalInfo, 1); sig_info->name = sig->name; sig_info->signatures = sig->signature; sig_info->path = info->path; sig_info->iface = info->iface; sig_info->callback = func; JSValueProtect(ctx, func); g_hash_table_insert(__sig_info_hash, key, sig_info); return GPOINTER_TO_INT(func); }
void addAccessibilityNotificationHandler(AccessibilityNotificationHandler* notificationHandler) { if (!notificationHandler) return; #if PLATFORM(GTK) JSGlobalContextRef jsContext = webkit_web_frame_get_global_context(mainFrame); #else JSContextRef jsContext = 0; #endif if (!jsContext) return; JSValueProtect(jsContext, notificationHandler->notificationFunctionCallback()); // Check if this notification handler is related to a specific element. if (notificationHandler->platformElement()) { if (notificationHandlers.contains(notificationHandler->platformElement())) { JSValueUnprotect(jsContext, notificationHandlers.find(notificationHandler->platformElement())->value->notificationFunctionCallback()); notificationHandlers.remove(notificationHandler->platformElement()); } notificationHandlers.add(notificationHandler->platformElement(), notificationHandler); } else { if (notificationHandlers.contains(GlobalNotificationKey)) { JSValueUnprotect(jsContext, notificationHandlers.find(GlobalNotificationKey)->value->notificationFunctionCallback()); notificationHandlers.remove(GlobalNotificationKey); } notificationHandlers.add(GlobalNotificationKey, notificationHandler); } connectAccessibilityCallbacks(); }
pdf_jsimp_obj *pdf_jsimp_new_obj(pdf_jsimp *imp, pdf_jsimp_type *type, void *natobj) { fz_context *ctx = imp->ctx; pdf_jsimp_obj *obj = fz_malloc_struct(ctx, pdf_jsimp_obj); priv_data *pdata = NULL; fz_var(pdata); fz_try(ctx) { pdata = fz_malloc_struct(ctx, priv_data); pdata->type = type; pdata->natobj = natobj; obj->ref = JSObjectMake(imp->jscore_ctx, imp->class_ref, pdata); if (obj->ref == NULL) fz_throw(ctx, FZ_ERROR_GENERIC, "JSObjectMake failed"); JSValueProtect(imp->jscore_ctx, obj->ref); } fz_catch(ctx) { fz_free(ctx, pdata); fz_free(ctx, obj); fz_rethrow(ctx); } return obj; }
Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) : cy::Functor(type, NULL), context_(CYGetJSContext(context)), function_(function) { //XXX:JSGlobalContextRetain(context_); JSValueProtect(context_, function_); }
// For interoperability with the JavaScriptCore C API. JSObject::JSObject(const JSContext& js_context, JSObjectRef js_object_ref) : js_context__(js_context) , js_object_ref__(js_object_ref) { HAL_LOG_TRACE("JSObject:: ctor 2 ", this); HAL_LOG_TRACE("JSObject:: retain ", js_object_ref__, " for ", this); JSValueProtect(static_cast<JSContextRef>(js_context__), js_object_ref__); RegisterJSContext(static_cast<JSContextRef>(js_context__), js_object_ref__); }
JSObject& JSObject::operator=(const JSObject& other) { JSValueUnprotect(ctx_, instance_); ctx_ = other.ctx_; instance_ = other.instance_; JSValueProtect(ctx_, instance_); return *this; }
jsc::Value &jsc::Value::operator = (const Value &other) { if (value != nullptr) { JSValueUnprotect(context, value); } value = other.value; JSValueProtect(context, value); return *this; }
jsc::Value &jsc::Value::operator = (JSValueRef newValue) { if (value != nullptr) { JSValueUnprotect(context, value); } value = newValue; JSValueProtect(context, value); return *this; }
EJ_BIND_GET( EJBindingImageData, data, ctx ) { if( !dataArray ) { int count = m_imageData->width * m_imageData->height * 4; dataArray = ByteArrayToJSObject(ctx, m_imageData->pixels, count); JSValueProtect(ctx, dataArray); } return dataArray; }
CYProtect(JSContextRef context, JSObjectRef object) : context_(CYGetJSContext(context)), object_(object) { //XXX:JSGlobalContextRetain(context_); if (object_ != NULL) JSValueProtect(context_, object_); }
static pdf_jsimp_obj *wrap_val(pdf_jsimp *imp, JSValueRef ref) { pdf_jsimp_obj *obj = fz_malloc_struct(imp->ctx, pdf_jsimp_obj); obj->ref = ref; JSValueProtect(imp->jscore_ctx, ref); return obj; }
JSObject::JSObject(const JSContext& js_context, const JSClass& js_class, void* private_data) : js_context__(js_context) , js_object_ref__(JSObjectMake(static_cast<JSContextRef>(js_context), static_cast<JSClassRef>(js_class), private_data)) { HAL_LOG_TRACE("JSObject:: ctor 1 ", this); HAL_LOG_TRACE("JSObject:: retain ", js_object_ref__, " (implicit) for ", this); JSValueProtect(static_cast<JSContextRef>(js_context__), js_object_ref__); RegisterJSContext(static_cast<JSContextRef>(js_context__), js_object_ref__); }
CYOwned(void *value, JSContextRef context, JSObjectRef owner) : CYValue(value), context_(CYGetJSContext(context)), owner_(owner) { //XXX:JSGlobalContextRetain(context_); if (owner_ != NULL) JSValueProtect(context_, owner_); }
JSFunction& JSFunction::operator=(const JSFunction& other) { if (instance_) JSValueUnprotect(ctx_, instance_); ctx_ = other.ctx_; instance_ = other.instance_; JSValueProtect(ctx_, instance_); return *this; }
static void cacheLayoutTestControllerCallback(unsigned index, JSValueRef callback) { if (!callback) return; WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page()); JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame); JSValueProtect(context, callback); callbackMap().add(index, callback); }
void setScriptObject(JSContextRef context, JSObjectRef newScriptObject) { if (scriptContext && scriptObject) JSValueUnprotect(scriptContext, scriptObject); scriptObject = newScriptObject; scriptContext = context; ASSERT((context && newScriptObject) || (!context && !newScriptObject)); if (context && newScriptObject) JSValueProtect(context, newScriptObject); }
// // Returns the contents of the array as an array of JSValueRef. // The caller is responsible for deleting the array returned by this method. // JSValueRef* ArrayWrapper::AsArray() { size_t len = GetSize(); JSValueRef* array = new JSValueRef[len]; for (size_t i = 0; i < len; ++i) { array[i] = JSObjectGetPropertyAtIndex(g_ctx, m_arr, (int) i, NULL); JSValueProtect(g_ctx, array[i]); } return array; }
void BB::PatchCollection::managePatch(BB::Patch& patch) { if (!this->m_patches.insert(&patch).second) { std::cerr << "Could not manage patch, perhaps it is already managed!" << std::endl; } else { JSContextRef ctx; ctx = this->m_context.context(); JSValueProtect(ctx, patch.object()); } }
bool IsArray(JSObjectRef obj) { if (g_fnxIsArray == NULL) { JSStringRef fnScript = JSStringCreateWithUTF8CString("return arguments[0] instanceof Array"); g_fnxIsArray = JSObjectMakeFunction(g_ctx, NULL, 0, NULL, fnScript, NULL, 0, NULL); JSValueProtect(g_ctx, g_fnxIsArray); JSStringRelease(fnScript); } JSValueRef isArray = JSObjectCallAsFunction(g_ctx, g_fnxIsArray, NULL, 1, (JSValueRef*) &obj, NULL); return JSValueToBoolean(g_ctx, isArray); }
JSValue::JSValue(const JSContext& js_context, const JSString& js_string, bool parse_as_json) : js_context__(js_context) { HAL_LOG_TRACE("JSValue:: ctor 1 ", this); if (parse_as_json) { js_value_ref__ = JSValueMakeFromJSONString(static_cast<JSContextRef>(js_context), static_cast<JSStringRef>(js_string)); if (!js_value_ref__) { const std::string message = "Input is not a valid JSON string: " + to_string(js_string); detail::ThrowRuntimeError("JSValue", message); } } else { js_value_ref__ = JSValueMakeString(static_cast<JSContextRef>(js_context__), static_cast<JSStringRef>(js_string)); } HAL_LOG_TRACE("JSValue:: retain ", js_value_ref__, " for ", this); JSValueProtect(static_cast<JSContextRef>(js_context__), js_value_ref__); }
static GumJscExceptionHandler * gum_jsc_exception_handler_new (JSObjectRef callback, GumJscCore * core) { GumJscExceptionHandler * handler; handler = g_slice_new (GumJscExceptionHandler); JSValueProtect (core->ctx, callback); handler->callback = callback; handler->core = core; gum_exceptor_add (core->exceptor, gum_jsc_exception_handler_on_exception, handler); return handler; }
NATIVE(JSValue,void,protect) (PARAMS, jlong ctxRef, jlong valueRef) { JSValueProtect( ((JSContextWrapper*)ctxRef)->context, (JSValueRef)valueRef ); /* JSContextWrapper *wrapper = (JSContextWrapper *)ctxRef; struct msg_t { JSContextRef ctxRef; JSValueRef valueRef; }; msg_t *msg = new msg_t; msg->ctxRef = wrapper->context; msg->valueRef = (JSValueRef)valueRef; wrapper->worker_q->async([](void *msg) { msg_t *m = (msg_t *)msg; JSValueProtect(m->ctxRef, m->valueRef); delete m; },msg); */ }