Beispiel #1
0
 // 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__);
 }
Beispiel #2
0
 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__);
 }
Beispiel #3
0
 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__);
 }
Beispiel #4
0
 JSValue& JSValue::operator=(JSValue rhs) {
   HAL_JSVALUE_LOCK_GUARD;
   HAL_LOG_TRACE("JSValue:: copy assignment ", this);
   // JSValues can only be copied between contexts within the same
   // context group.
   if (js_context__.get_context_group() != rhs.js_context__.get_context_group()) {
     detail::ThrowRuntimeError("JSValue", "JSValues must belong to JSContexts within the same JSContextGroup to be shared and exchanged.");
   }
   
   swap(rhs);
   return *this;
 }
Beispiel #5
0
 JSObject JSObject::FindJSObject(JSContextRef js_context_ref, JSObjectRef js_object_ref) {
   HAL_JSOBJECT_LOCK_GUARD_STATIC;
   const auto key      = reinterpret_cast<std::intptr_t>(js_object_ref);
   const auto position = js_object_ref_to_js_context_ref_map__.find(key);
   const bool found    = position != js_object_ref_to_js_context_ref_map__.end();
   
   // precondition
   if (found) {
     js_context_ref = reinterpret_cast<JSContextRef>(std::get<0>(position -> second));
   }
   
   HAL_LOG_TRACE("JSObject::FindJSObject: found = ", found, " for JSObjectRef ", js_object_ref, ", JSContextRef = ", js_context_ref);
   
   return JSObject(JSContext(js_context_ref), js_object_ref);
 }
Beispiel #6
0
  JSObject JSObject::FindJSObjectFromPrivateData(JSContext js_context, void* private_data) {
    HAL_JSOBJECT_LOCK_GUARD_STATIC;
    const auto key      = reinterpret_cast<std::intptr_t>(private_data);
    const auto position = js_private_data_to_js_object_ref_map__.find(key);
    const bool found    = position != js_private_data_to_js_object_ref_map__.end();

    // This could happen when owner object is gargabe collected while executing async operation.
    // This Error object will be only used internally to see if object is found or not.
    if (!found) {
      return js_context.CreateError();
    }

    JSObjectRef js_object_ref = reinterpret_cast<JSObjectRef>(position -> second);
    HAL_LOG_TRACE("JSObject::FindJSObjectFromPrivateData: found = ", found, " for data = ", key, ", JSObjectRef = ", js_object_ref);

    return FindJSObject(static_cast<JSContextRef>(js_context), js_object_ref);
  }