Ejemplo n.º 1
0
static void
settings_update(JSContext *cx, js_setting_t *jss, jsval v)
{
  jsval cb, *argv, result;
  void *mark;
  JS_SetProperty(cx, JSVAL_TO_OBJECT(jss->jss_obj), "value", &v);
  
  JS_GetProperty(cx, JSVAL_TO_OBJECT(jss->jss_obj), "callback", &cb);
  argv = JS_PushArguments(cx, &mark, "v", v);
  JS_CallFunctionValue(cx, NULL, cb, 1, argv, &result);
  JS_PopArguments(cx, mark);
}
Ejemplo n.º 2
0
static void
TestArgFormatter(JSContext* jscontext, JSObject* glob, nsIXPConnect* xpc)
{
    jsval* argv;
    void* mark;

    const char*                  a_in = "some string";
    nsCOMPtr<nsITestXPCFoo>      b_in = new nsTestXPCFoo();
    nsCOMPtr<nsIWritableVariant> c_in = do_CreateInstance("@mozilla.org/variant;1"); 
    static NS_NAMED_LITERAL_STRING(d_in, "foo bar");
    const char*                  e_in = "another meaningless chunck of text";
    

    char*                   a_out;
    nsCOMPtr<nsISupports>   b_out;
    nsCOMPtr<nsIVariant>    c_out;
    nsAutoString            d_out;
    char*                   e_out;

    nsCOMPtr<nsITestXPCFoo> specified;
    PRInt32                 val;

    printf("ArgumentFormatter test: ");

    if(!b_in || !c_in || NS_FAILED(c_in->SetAsInt32(5)))
    {
        printf(" failed to construct test objects -- FAILED!\n");
        return;
    }

    argv = JS_PushArguments(jscontext, &mark, "s %ip %iv %is s",
                            a_in, 
                            &NS_GET_IID(nsITestXPCFoo2), b_in.get(), 
                            c_in.get(),
                            static_cast<const nsAString*>(&d_in), 
                            e_in);

    if(!argv)
    {
        printf(" could not convert from native to JS -- FAILED!\n");
        return;
    }

    if(!JS_ConvertArguments(jscontext, 5, argv, "s %ip %iv %is s",
                            &a_out, 
                            static_cast<nsISupports**>(getter_AddRefs(b_out)), 
                            static_cast<nsIVariant**>(getter_AddRefs(c_out)),
                            static_cast<nsAString*>(&d_out), 
                            &e_out))
    {
        printf(" could not convert from JS to native -- FAILED!\n");
        goto out;
    }

    if(!b_out)
    {
        printf(" JS to native for %%ip returned NULL -- FAILED!\n");
        goto out;
    }

    specified = do_QueryInterface(b_out);
    if(!specified)
    {
        printf(" could not QI value JS to native returned -- FAILED!\n");
        goto out;
    }

    if(specified.get() != b_in.get())
    {
        printf(" JS to native returned wrong value -- FAILED!\n");
        goto out;
    }

    if(!c_out)
    {
        printf(" JS to native for %%iv returned NULL -- FAILED!\n");
        goto out;
    }

    if(NS_FAILED(c_out->GetAsInt32(&val)) || val != 5)
    {
        printf(" JS to native for %%iv holds wrong value -- FAILED!\n");
        goto out;
    }

    if(!d_in.Equals(d_out))
    {
        printf(" JS to native for %%is returned the wrong value -- FAILED!\n");
        goto out;
    }

    if(!strcmp(a_in, a_out) && !strcmp(e_in, e_out))
        printf("passed\n");
    else
        printf(" conversion OK, but surrounding was mangled -- FAILED!\n");

out:
    JS_PopArguments(jscontext, mark);
}