inline js_proxy_t *js_get_or_create_proxy(JSContext *cx, T *native_obj) {
    auto proxy = jsb_get_native_proxy(native_obj);
    if (!proxy) {
        js_type_class_t *typeProxy = js_get_type_from_native<T>(native_obj);
        // Return NULL if can't find its type rather than making an assert.
//        assert(typeProxy);
        if (!typeProxy) {
            CCLOGINFO("Could not find the type of native object.");
            return NULL;
        }

        JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET

        JS::RootedObject proto(cx, typeProxy->proto.ref().get());
        JS::RootedObject parent(cx, typeProxy->parentProto.ref().get());
        JS::RootedObject js_obj(cx, JS_NewObject(cx, typeProxy->jsclass, proto, parent));
        proxy = jsb_new_proxy(native_obj, js_obj);
#ifdef DEBUG
        AddNamedObjectRoot(cx, &proxy->obj, typeid(*native_obj).name());
#else
        AddObjectRoot(cx, &proxy->obj);
#endif
        return proxy;
    } else {
        return proxy;
    }
    return NULL;
}
Exemple #2
0
static void
js_value (JSContextRef ctx, JSValueRef value, JsonNode ** v)
{
  switch (JSValueGetType (ctx, value))
    {
    case kJSTypeUndefined:
    case kJSTypeNull:
      *v = json_node_new (JSON_NODE_NULL);

      break;

    case kJSTypeBoolean:
      *v = json_node_new (JSON_NODE_VALUE);

      json_node_set_boolean (*v,
                                 JSValueToBoolean (ctx,
                                                   value) ==
                                 true ? TRUE : FALSE);
      break;

    case kJSTypeNumber:
      *v = json_node_new (JSON_NODE_VALUE);

      json_node_set_double (*v,
                                (gdouble) JSValueToNumber (ctx, value, NULL));
      break;

    case kJSTypeString:
      {
        JSStringRef string;
        gchar *str;

        string = JSValueToStringCopy (ctx, value, NULL);
        str = js_string (string);
        JSStringRelease (string);

        *v = json_node_new (JSON_NODE_VALUE);

        json_node_set_string (*v, str);

        g_free (str);
        break;
      }

    case kJSTypeObject:
      {
        *v = json_node_new (JSON_NODE_OBJECT);
        JsonObject *o = json_object_new ();

        js_obj (ctx, JSValueToObject (ctx, value, NULL), o);

        json_node_take_object (*v, o);

        break;
      }
    }

  /* FIXME: array?!? integer?!?
            -> probably arrays are considered instances of Array() Javascript object ?!

            see http://developer.apple.com/library/mac/#documentation/Carbon/Reference/WebKit_JavaScriptCore_Ref/JSValueRef_h/index.html%23//apple_ref/c/func/JSValueGetType */

  debug_print_json_node ( "js_value(): ", *v );
}