Esempio n. 1
0
void CStdDeserializer::AddScriptBackref(JSObject* obj)
{
	std::pair<std::map<u32, JSObject*>::iterator, bool> it = m_ScriptBackrefs.insert(std::make_pair((u32)m_ScriptBackrefs.size()+1, obj));
	ENSURE(it.second);
	if (!JS_AddObjectRoot(m_ScriptInterface.GetContext(), &it.first->second))
		throw PSERROR_Deserialize_ScriptError("JS_AddRoot failed");
}
Esempio n. 2
0
GObject *
peas_extension_gjs_new (GType      exten_type,
                        GType     *interfaces,
                        JSContext *js_context,
                        JSObject  *js_object)
{
  PeasExtensionGjs *gexten;
  GType real_type;

  g_return_val_if_fail (js_context != NULL, NULL);
  g_return_val_if_fail (js_object != NULL, NULL);

  real_type = peas_extension_register_subclass (PEAS_TYPE_EXTENSION_GJS,
                                                interfaces);

  /* Already Warned */
  if (real_type == G_TYPE_INVALID)
    {
      g_free (interfaces);
      return NULL;
    }

  gexten = PEAS_EXTENSION_GJS (g_object_new (real_type, NULL));

  gexten->js_context = js_context;
  gexten->js_object = js_object;
  PEAS_EXTENSION_WRAPPER (gexten)->exten_type = exten_type;
  PEAS_EXTENSION_WRAPPER (gexten)->interfaces = interfaces;
  JS_AddObjectRoot (gexten->js_context, &gexten->js_object);

  return G_OBJECT (gexten);
}
Esempio n. 3
0
	BuildDirEntListState(JSContext* cx_)
		: cx(cx_)
	{
		filename_array = JS_NewArrayObject(cx, 0, NULL);
		JS_AddObjectRoot(cx, &filename_array);
		cur_idx = 0;
	}
Esempio n. 4
0
inline js_proxy_t *js_get_or_create_proxy(JSContext *cx, T *native_obj) {
    js_proxy_t *proxy;
    HASH_FIND_PTR(_native_js_global_ht, &native_obj, proxy);
    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;
        }
        
        JSObject* js_obj = JS_NewObject(cx, typeProxy->jsclass, typeProxy->proto, typeProxy->parentProto);
        proxy = jsb_new_proxy(native_obj, js_obj);
#ifdef DEBUG
        JS_AddNamedObjectRoot(cx, &proxy->obj, typeid(*native_obj).name());
#else
        JS_AddObjectRoot(cx, &proxy->obj);
#endif
        return proxy;
    } else {
        return proxy;
    }
    return NULL;
}
Esempio n. 5
0
static bool dummy_constructor(JSContext *cx, uint32_t argc, jsval *vp) {
    JS::RootedValue initializing(cx);
    bool isNewValid = true;
#if not $script_control_cpp
    JSObject* global = ScriptingCore::getInstance()->getGlobalObject();
	isNewValid = JS_GetProperty(cx, global, "initializing", &initializing) && JSVAL_TO_BOOLEAN(initializing);
#end if
	if (isNewValid)
	{
		TypeTest<T> t;
		js_type_class_t *typeClass = nullptr;
		std::string typeName = t.s_name();
		auto typeMapIter = _js_global_type_map.find(typeName);
		CCASSERT(typeMapIter != _js_global_type_map.end(), "Can't find the class type!");
		typeClass = typeMapIter->second;
		CCASSERT(typeClass, "The value is null.");

		JSObject *_tmp = JS_NewObject(cx, typeClass->jsclass, typeClass->proto, typeClass->parentProto);
	#if $script_control_cpp
		T* cobj = new T();
		js_proxy_t *pp = jsb_new_proxy(cobj, _tmp);
		JS_AddObjectRoot(cx, &pp->obj);
	#end if
		JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(_tmp));
		return true;
	}

#if not $script_control_cpp
    JS_ReportError(cx, "Don't use `new cc.XXX`, please use `cc.XXX.create` instead! ");
#end if
    return false;
}
Esempio n. 6
0
static JSBool
resolve_namespace_object(JSContext  *context,
                         JSObject   *repo_obj,
                         jsid        ns_id,
                         const char *ns_name)
{
    GIRepository *repo;
    GError *error;
    char *version;
    JSObject *override;
    jsval result;
    JSObject *gi_namespace = NULL;
    JSBool ret = JS_FALSE;
    char *check_name = NULL;

    if (g_strcmp0 (ns_name, "GMenu") == 0) {
        check_name = g_strdup ("CMenu");
    } else {
        check_name = g_strdup (ns_name);
    }

    JS_BeginRequest(context);

    if (!get_version_for_ns(context, repo_obj, ns_id, &version))
        goto out;

    repo = g_irepository_get_default();

    error = NULL;
    g_irepository_require(repo, check_name, version, (GIRepositoryLoadFlags) 0, &error);
    if (error != NULL) {
        gjs_throw(context,
                  "Requiring %s, version %s: %s",
                  check_name, version?version:"none", error->message);

        g_error_free(error);
        g_free(version);
        goto out;
    }

    g_free(version);

    /* Defines a property on "obj" (the javascript repo object)
     * with the given namespace name, pointing to that namespace
     * in the repo.
     */
    gi_namespace = gjs_create_ns(context, check_name);
    JS_AddObjectRoot(context, &gi_namespace);

    /* Define the property early, to avoid reentrancy issues if
       the override module looks for namespaces that import this */
    if (!JS_DefineProperty(context, repo_obj,
                           ns_name, OBJECT_TO_JSVAL(gi_namespace),
                           NULL, NULL,
                           GJS_MODULE_PROP_FLAGS))
        g_error("no memory to define ns property");

    override = lookup_override_function(context, ns_id);
static void _gjs_builder_connect_func (GtkBuilder *builder,
                                       GObject *object,
                                       const gchar *signal_name,
                                       const gchar *handler_name,
                                       GObject *connect_object,
                                       GConnectFlags flags,
                                       gpointer user_data)
{
    builder_ud *priv = (builder_ud *)user_data;
    JSContext *ctx = priv->ctx;
    JSObject *obj = priv->obj;
    GClosure *closure;
    JSObject *callable;
    builder_cd *cd;
    closure_data *c;
    jsval func;

    if (!gjs_object_get_property (ctx, obj, handler_name, &func))
        return;

    if (!JSVAL_IS_OBJECT(func))
        return;

    callable = JSVAL_TO_OBJECT (func);
    if (!JS_ObjectIsFunction(ctx, callable))
        return;

    /* Protect from garbage collection. */
    cd = g_object_get_data (G_OBJECT (builder), GJS_BUILDER_CLOSURE_KEY);
    if (!cd) {
        cd = g_new0 (builder_cd, 1);
        cd->context = ctx;
        cd->closures = NULL;
        g_object_set_data_full (G_OBJECT (builder),
                                GJS_BUILDER_CLOSURE_KEY,
                                cd,
                                (GDestroyNotify) _builder_cd_free);
    }

    g_assert (cd->context == ctx);

    c = g_new0 (closure_data, 1);
    c->jsobj = callable;
    cd->closures = g_slist_prepend (cd->closures, c);
    JS_AddObjectRoot (ctx, &c->jsobj);

    closure = gjs_closure_new_for_signal (ctx,
                                          callable,
                                          "signal handler (GtkBuilder)",
                                          0);
    if (connect_object != NULL)
        g_object_watch_closure (connect_object, closure);

    c->object = g_object_ref (object);
    c->handler_id = g_signal_connect_closure (object, signal_name, closure, FALSE);
}
Esempio n. 8
0
JS_BINDED_FUNC_IMPL(FakeCanvas, addEventListener) {
	jsval* argv = JS_ARGV(cx, vp);
	if (argc >= 2 && argv[0].isString()) {
		JSStringWrapper str(argv[0].toString(), cx);
		if (argv[1].isObject()) {
			JSObject* cb = argv[1].toObjectOrNull();
			JS_AddObjectRoot(cx, &cb);
			this->listeners[(const char*)str] = (void *)cb;
		}
	}
	return JS_TRUE;
}
Esempio n. 9
0
void postEvent (JSContext* jsCtx, AmityEvent* event)
{
    JS_AddObjectRoot(jsCtx, &event->obj);
    JS_AddValueRoot(jsCtx, &event->param);

    SDL_Event sdlEvent;
    sdlEvent.type = SDL_USEREVENT;
    sdlEvent.user.code = AMITY_EVENT_SDL_CODE;
    sdlEvent.user.data1 = event;
    sdlEvent.user.data2 = NULL;
    SDL_PushEvent(&sdlEvent);
}
void JSArmatureWrapper::setJSCallbackThis(jsval _jsThisObj)
{
    JSCallbackWrapper::setJSCallbackThis(_jsThisObj);

    JSObject *thisObj = JSVAL_TO_OBJECT(_jsThisObj);
    js_proxy *p = jsb_get_js_proxy(thisObj);
    if (!p)
    {
        JSContext *cx = ScriptingCore::getInstance()->getGlobalContext();
        JS_AddObjectRoot(cx, &thisObj);
        m_bNeedUnroot = true;
    }
}
inline js_proxy_t *js_get_or_create_proxy(JSContext *cx, T *native_obj) {
    js_proxy_t *proxy;
    HASH_FIND_PTR(_native_js_global_ht, &native_obj, proxy);
    if (!proxy) {
        js_type_class_t *typeProxy = js_get_type_from_native<T>(native_obj);
        assert(typeProxy);
        JSObject* js_obj = JS_NewObject(cx, typeProxy->jsclass, typeProxy->proto, typeProxy->parentProto);
        JS_NEW_PROXY(proxy, native_obj, js_obj);
#ifdef DEBUG
        JS_AddNamedObjectRoot(cx, &proxy->obj, typeid(*native_obj).name());
#else
        JS_AddObjectRoot(cx, &proxy->obj);
#endif
        return proxy;
    } else {
        return proxy;
    }
    return NULL;
}
Esempio n. 12
0
static JSBool dummy_constructor(JSContext *cx, uint32_t argc, jsval *vp) {
	TypeTest<T> t;
	T* cobj = new T();
	cocos2d::CCObject *_ccobj = dynamic_cast<cocos2d::CCObject *>(cobj);
	if (_ccobj) {
		_ccobj->autorelease();
	}
	js_type_class_t *p;
	uint32_t typeId = t.s_id();
	HASH_FIND_INT(_js_global_type_ht, &typeId, p);
	assert(p);
	JSObject *_tmp = JS_NewObject(cx, p->jsclass, p->proto, p->parentProto);
	js_proxy_t *pp;
	JS_NEW_PROXY(pp, cobj, _tmp);
	JS_AddObjectRoot(cx, &pp->obj);
	JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(_tmp));

	return JS_TRUE;
}
static JSBool sys_getitems(JSContext *cx, unsigned argc, jsval *vp)
{
    gitem_t	*it;
    JSObject *j;
    jsval val;
    int i;
    jsval returnValue;
    JSObject *arr = JS_NewArrayObject(cx, 0, NULL);
    JS_AddObjectRoot(cx, &arr);

    for ( i=0, it = bg_itemlist + 1 ; it->classname ; it++,i++) {
        j = JS_NewObject(cx, NULL, NULL, NULL);
        JS_Object_SetItem(cx, j, it);
        val = OBJECT_TO_JSVAL(j);
        JS_SetElement(cx,arr,i,&val);
    }

    JS_RemoveObjectRoot(cx, &arr);
    returnValue = OBJECT_TO_JSVAL(arr);
    JS_SET_RVAL(cx, vp, returnValue);
    return JS_TRUE;
}
Esempio n. 14
0
static bool dummy_constructor(JSContext *cx, uint32_t argc, jsval *vp) {
    JS::RootedValue initializing(cx);
    bool isNewValid = true;
    if (isNewValid)
    {
        TypeTest<T> t;
        js_type_class_t *typeClass = nullptr;
        std::string typeName = t.s_name();
        auto typeMapIter = _js_global_type_map.find(typeName);
        CCASSERT(typeMapIter != _js_global_type_map.end(), "Can't find the class type!");
        typeClass = typeMapIter->second;
        CCASSERT(typeClass, "The value is null.");

        JSObject *_tmp = JS_NewObject(cx, typeClass->jsclass, typeClass->proto, typeClass->parentProto);
        T* cobj = new T();
        js_proxy_t *pp = jsb_new_proxy(cobj, _tmp);
        JS_AddObjectRoot(cx, &pp->obj);
        JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(_tmp));
        return true;
    }

    return false;
}
Esempio n. 15
0
void JsSchedule::SetCallbackData(JSObject *callbackValue) {
    this->callbackValue = callbackValue;
    JS_AddObjectRoot(JavaScriptEngine::ShareInstance().ShareContext(),
                     &this->callbackValue);
}
Esempio n. 16
0
static JSBool
setDash_func(JSContext *context,
             unsigned   argc,
             jsval     *vp)
{
    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
    JSObject *obj = JSVAL_TO_OBJECT(argv.thisv());

    guint i;
    cairo_t *cr;
    JSObject *dashes;
    double offset;
    JSBool retval = JS_FALSE;
    guint len;
    GArray *dashes_c = NULL;

    if (!gjs_parse_call_args(context, "setDash", "of", argv,
                        "dashes", &dashes, "offset", &offset))
        return JS_FALSE;

    JS_AddObjectRoot(context, &dashes);

    if (!JS_IsArrayObject(context, dashes)) {
        gjs_throw(context, "dashes must be an array");
        goto out;
    }

    if (!JS_GetArrayLength(context, dashes, &len)) {
        gjs_throw(context, "Can't get length of dashes");
        goto out;
    }

    dashes_c = g_array_sized_new (FALSE, FALSE, sizeof(double), len);
    for (i = 0; i < len; ++i) {
        jsval elem;
        double b;

        elem = JSVAL_VOID;
        if (!JS_GetElement(context, dashes, i, &elem)) {
            goto out;
        }
        if (JSVAL_IS_VOID(elem))
            continue;

        if (!JS_ValueToNumber(context, elem, &b))
            goto out;
        if (b <= 0) {
            gjs_throw(context, "Dash value must be positive");
            goto out;
        }

        g_array_append_val(dashes_c, b);
    }

    cr = gjs_cairo_context_get_context(context, obj);
    cairo_set_dash(cr, (double*)dashes_c->data, dashes_c->len, offset);
    argv.rval().set(JSVAL_VOID);
    retval = JS_TRUE;
 out:
    if (dashes_c != NULL)
        g_array_free (dashes_c, TRUE);
    JS_RemoveObjectRoot(context, &dashes);
    return retval;
}
Esempio n. 17
0
/* fromArray() function implementation */
static JSBool
from_array_func(JSContext *context,
                unsigned   argc,
                jsval     *vp)
{
    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
    ByteArrayInstance *priv;
    guint32 len;
    guint32 i;
    JSObject *obj;
    JSBool ret = JS_FALSE;

    obj = byte_array_new(context);
    if (obj == NULL)
        return JS_FALSE;

    JS_AddObjectRoot(context, &obj);

    priv = priv_from_js(context, obj);
    g_assert (priv != NULL);

    g_assert(argc > 0); /* because we specified min args 1 */

    priv->array = gjs_g_byte_array_new(0);

    if (!JS_IsArrayObject(context, JSVAL_TO_OBJECT(argv[0]))) {
        gjs_throw(context,
                  "byteArray.fromArray() called with non-array as first arg");
        goto out;
    }

    if (!JS_GetArrayLength(context, JSVAL_TO_OBJECT(argv[0]), &len)) {
        gjs_throw(context,
                  "byteArray.fromArray() can't get length of first array arg");
        goto out;
    }

    g_byte_array_set_size(priv->array, len);

    for (i = 0; i < len; ++i) {
        jsval elem;
        guint8 b;

        elem = JSVAL_VOID;
        if (!JS_GetElement(context, JSVAL_TO_OBJECT(argv[0]), i, &elem)) {
            /* this means there was an exception, while elem == JSVAL_VOID
             * means no element found
             */
            goto out;
        }

        if (JSVAL_IS_VOID(elem))
            continue;

        if (!gjs_value_to_byte(context, elem, &b))
            goto out;

        g_array_index(priv->array, guint8, i) = b;
    }

    ret = JS_TRUE;
    argv.rval().set(OBJECT_TO_JSVAL(obj));
 out:
    JS_RemoveObjectRoot(context, &obj);
    return ret;
}
Esempio n. 18
0
/* fromString() function implementation */
static JSBool
from_string_func(JSContext *context,
                 unsigned   argc,
                 jsval     *vp)
{
    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
    ByteArrayInstance *priv;
    char *encoding;
    gboolean encoding_is_utf8;
    JSObject *obj;
    JSBool retval = JS_FALSE;

    obj = byte_array_new(context);
    if (obj == NULL)
        return JS_FALSE;

    JS_AddObjectRoot(context, &obj);

    priv = priv_from_js(context, obj);
    g_assert (priv != NULL);

    g_assert(argc > 0); /* because we specified min args 1 */

    priv->array = gjs_g_byte_array_new(0);

    if (!JSVAL_IS_STRING(argv[0])) {
        gjs_throw(context,
                  "byteArray.fromString() called with non-string as first arg");
        goto out;
    }

    if (argc > 1 &&
        JSVAL_IS_STRING(argv[1])) {
        if (!gjs_string_to_utf8(context, argv[1], &encoding))
            goto out;

        /* maybe we should be smarter about utf8 synonyms here.
         * doesn't matter much though. encoding_is_utf8 is
         * just an optimization anyway.
         */
        if (strcmp(encoding, "UTF-8") == 0) {
            encoding_is_utf8 = TRUE;
            g_free(encoding);
            encoding = NULL;
        } else {
            encoding_is_utf8 = FALSE;
        }
    } else {
        encoding_is_utf8 = TRUE;
    }

    if (encoding_is_utf8) {
        /* optimization? avoids iconv overhead and runs
         * libmozjs hardwired utf16-to-utf8.
         */
        char *utf8 = NULL;
        if (!gjs_string_to_utf8(context,
                                argv[0],
                                &utf8))
            goto out;

        g_byte_array_set_size(priv->array, 0);
        g_byte_array_append(priv->array, (guint8*) utf8, strlen(utf8));
        g_free(utf8);
    } else {
        char *encoded;
        gsize bytes_written;
        GError *error;
        const jschar *u16_chars;
        gsize u16_len;

        u16_chars = JS_GetStringCharsAndLength(context, JSVAL_TO_STRING(argv[0]), &u16_len);
        if (u16_chars == NULL)
            goto out;

        error = NULL;
        encoded = g_convert((char*) u16_chars,
                            u16_len * 2,
                            encoding, /* to_encoding */
                            "UTF-16", /* from_encoding */
                            NULL, /* bytes read */
                            &bytes_written,
                            &error);
        g_free(encoding);
        if (encoded == NULL) {
            /* frees the GError */
            gjs_throw_g_error(context, error);
            goto out;
        }

        g_byte_array_set_size(priv->array, 0);
        g_byte_array_append(priv->array, (guint8*) encoded, bytes_written);

        g_free(encoded);
    }

    argv.rval().set(OBJECT_TO_JSVAL(obj));

    retval = JS_TRUE;
 out:
    JS_RemoveObjectRoot(context, &obj);
    return retval;
}