t = "try{Components.classes.allocator.getService(); print('passed');}catch(e){failed = true; print('failed');}";
t = "try{Components.classes.nsIXPConnect.getService(); print('passed');}catch(e){failed = true; print('failed');}";
int main(){    JSRuntime *rt;    JSContext *jscontext;    JSObject *glob;    gErrFile = stderr;    gOutFile = stdout;    SetupRegistry();    rt = JS_NewRuntime(8L * 1024L * 1024L);    if (!rt)        return 1;    jscontext = JS_NewContext(rt, 8192);    if (!jscontext)        return 1;    JS_SetErrorReporter(jscontext, my_ErrorReporter);    nsIXPConnect* xpc = GetXPConnect();    if(!xpc)    {        printf("XPC_GetXPConnect() returned NULL!\n");        return 1;    }#if 0    // old code where global object was plain object    glob = JS_NewObject(jscontext, &global_class, NULL, NULL);    if (!glob)        return 1;    if (!JS_InitStandardClasses(jscontext, glob))        return 1;    if (!JS_DefineFunctions(jscontext, glob, glob_functions))        return 1;    xpc->InitJSContext(jscontext, glob, JS_TRUE);#if 0    nsIXPCComponents* comp = XPC_GetXPConnectComponentsObject();    if(!comp)    {        printf("failed to create Components native object");        return 1;    }    nsIXPConnectWrappedNative* comp_wrapper;    if(NS_FAILED(xpc->WrapNative(jscontext, comp,                              nsIXPCComponents::GetIID(), &comp_wrapper)))    {        printf("failed to build wrapper for Components native object");        return 1;    }    JSObject* comp_jsobj;    comp_wrapper->GetJSObject(&comp_jsobj);    jsval comp_jsval = OBJECT_TO_JSVAL(comp_jsobj);    JS_SetProperty(jscontext, glob, "Components", &comp_jsval);    NS_RELEASE(comp_wrapper);    NS_RELEASE(comp);#endif    char* txt[] = {      "load('simpletest.js');",      0,    };    jsval rval;    for(char** p = txt; *p; p++)        JS_EvaluateScript(jscontext, glob, *p, strlen(*p), "builtin", 1, &rval);//    XPC_DUMP(xpc, 20);#else    nsTestXPCFoo* foo = new nsTestXPCFoo();//    nsXPCVarient v[2];//    v[0].type = nsXPCType::T_I32; v[0].val.i32 = 1;//    v[1].type = nsXPCType::T_I32; v[1].val.i32 = 2;//    XPC_TestInvoke(foo, 3, 2, v);//    XPC_TestInvoke(foo, 4, 0, NULL);    nsIXPConnectWrappedNative* wrapper;    nsIXPConnectWrappedNative* wrapper2;    nsIXPConnectWrappedNative* wrapper3;    nsIXPConnectWrappedNative* fool_wrapper = NULL;/*    if(NS_SUCCEEDED(xpc->WrapNative(jscontext, foo, nsITestXPCFoo::GetIID(), &wrapper)))*/    // new code where global object is a wrapped xpcom object    if(NS_SUCCEEDED(xpc->InitJSContextWithNewWrappedGlobal(                jscontext, foo, nsITestXPCFoo::GetIID(), JS_TRUE, &wrapper)))    {        wrapper->GetJSObject(&glob);        JS_DefineFunctions(jscontext, glob, glob_functions);#if 0        nsIXPCComponents* comp = XPC_GetXPConnectComponentsObject();        if(!comp)        {            printf("failed to create Components native object");            return 1;        }        nsIXPConnectWrappedNative* comp_wrapper;        if(NS_FAILED(xpc->WrapNative(jscontext, comp,                                  nsIXPCComponents::GetIID(), &comp_wrapper)))        {            printf("failed to build wrapper for Components native object");            return 1;        }        JSObject* comp_jsobj;        comp_wrapper->GetJSObject(&comp_jsobj);        jsval comp_jsval = OBJECT_TO_JSVAL(comp_jsobj);        JS_SetProperty(jscontext, glob, "Components", &comp_jsval);        NS_RELEASE(comp_wrapper);        NS_RELEASE(comp);#endif        nsTestXPCFoo* fool = new nsTestXPCFoo();        xpc->WrapNative(jscontext, fool, nsITestXPCFoo2::GetIID(), &fool_wrapper);        if(NS_SUCCEEDED(xpc->WrapNative(jscontext, foo, nsITestXPCFoo2::GetIID(), &wrapper2)))        {            JSObject* js_obj;            nsISupports* com_obj;            jsval rval;            wrapper2->GetJSObject(&js_obj);            wrapper2->GetNative(&com_obj);            jsval v;            v = OBJECT_TO_JSVAL(js_obj);            JS_SetProperty(jscontext, glob, "foo", &v);            // add the reflected native echo object (sans error checking :)            nsIXPConnectWrappedNative* echo_wrapper;            JSObject* echo_jsobj;            jsval echo_jsval;            MyEcho* myEcho = new MyEcho();            xpc->WrapNative(jscontext, myEcho, nsIEcho::GetIID(), &echo_wrapper);            echo_wrapper->GetJSObject(&echo_jsobj);            echo_jsval = OBJECT_TO_JSVAL(echo_jsobj);            JS_SetProperty(jscontext, glob, "echo", &echo_jsval);            char* txt[] = {              "load('testxpc.js');",              0,            };            for(char** p = txt; *p; p++)                JS_EvaluateScript(jscontext, glob, *p, strlen(*p), "builtin", 1, &rval);            if(JS_GetProperty(jscontext, glob, "bar", &v) && JSVAL_IS_OBJECT(v))            {                JSObject* bar = JSVAL_TO_OBJECT(v);                nsISupports* wrapper3;                if(NS_SUCCEEDED(xpc->WrapJS(jscontext,                                       JSVAL_TO_OBJECT(v),                                       nsITestXPCFoo::GetIID(), &wrapper3)))                {                    nsITestXPCFoo* ptr = (nsITestXPCFoo*)wrapper3;                    int result;                    JSObject* test_js_obj;                    ptr->Test(11, 13, &result);                    printf("call to ptr->Test result: %s\n",                           result == 24 ? "passed" : "FAILED");                    nsIXPConnectWrappedJSMethods* methods;                    wrapper3->QueryInterface(nsIXPConnectWrappedJSMethods::GetIID(),                                            (void**) &methods);                    methods->GetJSObject(&test_js_obj);                    printf("call to methods->GetJSObject() : %s\n",                            test_js_obj == JSVAL_TO_OBJECT(v) ?                            "passed" : "FAILED" );                    char some_string[] = "some string here";                    char* answer = NULL;//                    ptr->GetFoo(&answer);//                    printf("Foo : %s\n", answer);                    ptr->SetFoo(some_string);                    ptr->GetFoo(&answer);                    printf("set/get property : %s\n",                           0 == strcmp(some_string, answer) ?                                "passed" : "FALIED");                    if(answer)                        nsAllocator::Free(answer);                    // dump to log test...//                    XPC_DUMP(xpc, 50);                    NS_RELEASE(methods);                    NS_RELEASE(wrapper3);                }            }            NS_RELEASE(com_obj);            NS_RELEASE(wrapper2);            NS_RELEASE(echo_wrapper);            NS_RELEASE(myEcho);        }//        NS_RELEASE(wrapper);    }    // XXX things are still rooted to the global object in the test scripts...    // dump to log test...//    XPC_LOG_ALWAYS((""));//    XPC_LOG_ALWAYS(("after running release object..."));//    XPC_LOG_ALWAYS((""));//    XPC_DUMP(xpc, 3);    {    // SecurityManager tests...    char* t;    jsval rval;    JSBool success = JS_TRUE;    MySecMan* sm = new MySecMan();    nsTestXPCFoo* foo3 = new nsTestXPCFoo();    if(!sm || ! foo3)    {        success = JS_FALSE;        printf("failed to create object!\n");        goto sm_test_done;    }    rval = JSVAL_FALSE;    JS_SetProperty(jscontext, glob, "failed", &rval);    printf("Individual SecurityManager tests...\n");    if(!NS_SUCCEEDED(xpc->SetSecurityManagerForJSContext(jscontext, sm,                                        nsIXPCSecurityManager::HOOK_ALL)))    {        success = JS_FALSE;        printf("SetSecurityManagerForJSContext failed!\n");        goto sm_test_done;    }    sm->SetMode(MySecMan::OK_ALL);    printf("  getService no veto: ");    t = "try{Components.classes.allocator.getService(); print('passed');}catch(e){failed = true; print('failed');}";    JS_EvaluateScript(jscontext, glob, t, strlen(t), "builtin", 1, &rval);    sm->SetMode(MySecMan::VETO_ALL);    printf("  getService with veto: ");    t = "try{Components.classes.allocator.getService(); failed = true; print('failed');}catch(e){print('passed');}";    JS_EvaluateScript(jscontext, glob, t, strlen(t), "builtin", 1, &rval);    sm->SetMode(MySecMan::OK_ALL);    printf("  createInstance no veto: ");    t = "try{Components.classes.nsIID.createInstance(Components.interfaces.nsIJSIID); print('passed');}catch(e){failed = true; print('failed');}";    JS_EvaluateScript(jscontext, glob, t, strlen(t), "builtin", 1, &rval);    sm->SetMode(MySecMan::VETO_ALL);    printf("  getService with veto: ");    t = "try{Components.classes.nsIID.createInstance(Components.interfaces.nsIJSIID); failed = true; print('failed');}catch(e){print('passed');}";    JS_EvaluateScript(jscontext, glob, t, strlen(t), "builtin", 1, &rval);    sm->SetMode(MySecMan::OK_ALL);    printf("  call method no veto: ");    t = "try{foo.Test2(); print(' : passed');}catch(e){failed = true; print(' : failed');}";    JS_EvaluateScript(jscontext, glob, t, strlen(t), "builtin", 1, &rval);    sm->SetMode(MySecMan::VETO_ALL);    printf("  call method with veto: ");    t = "try{foo.Test2(); failed = true; print(' : failed');}catch(e){print(' : passed');}";    JS_EvaluateScript(jscontext, glob, t, strlen(t), "builtin", 1, &rval);    sm->SetMode(MySecMan::OK_ALL);    printf("  get attribute no veto: ");    t = "try{foo.Foo; print(' : passed');}catch(e){failed = true; print(' : failed');}";    JS_EvaluateScript(jscontext, glob, t, strlen(t), "builtin", 1, &rval);    sm->SetMode(MySecMan::VETO_ALL);    printf("  get attribute with veto: ");    t = "try{foo.Foo; failed = true; print(' : failed');}catch(e){print(' : passed');}";    JS_EvaluateScript(jscontext, glob, t, strlen(t), "builtin", 1, &rval);    sm->SetMode(MySecMan::OK_ALL);    printf("  set attribute no veto: ");    t = "try{foo.Foo = 0; print(' : passed');}catch(e){failed = true; print(' : failed');}";    JS_EvaluateScript(jscontext, glob, t, strlen(t), "builtin", 1, &rval);    sm->SetMode(MySecMan::VETO_ALL);    printf("  set attribute with veto: ");    t = "try{foo.Foo = 0; failed = true; print(' : failed');}catch(e){print(' : passed');}";    JS_EvaluateScript(jscontext, glob, t, strlen(t), "builtin", 1, &rval);    sm->SetMode(MySecMan::OK_ALL);    printf("  build wrapper no veto: ");    if(NS_SUCCEEDED(xpc->WrapNative(jscontext, foo, nsITestXPCFoo2::GetIID(), &wrapper3)))    {        printf("passed\n");        NS_RELEASE(wrapper3);    }    else    {        success = JS_FALSE;        printf("failed\n");    }    sm->SetMode(MySecMan::VETO_ALL);    printf("  build wrapper with veto: ");    if(NS_SUCCEEDED(xpc->WrapNative(jscontext, foo3, nsITestXPCFoo2::GetIID(), &wrapper3)))    {        success = JS_FALSE;        printf("failed\n");        NS_RELEASE(wrapper3);    }    else    {        printf("passed\n");    }sm_test_done:    success = success && JS_GetProperty(jscontext, glob, "failed", &rval) && JSVAL_TRUE != rval;    printf("SecurityManager tests : %s\n", success ? "passed" : "failed");    NS_RELEASE(foo3);    }    {        // arg formatter test...        jsval* argv;        void* mark;        const char* a_in = "some string";        const char* b_in = "another meaningless chunck of text";        char* a_out;        char* b_out;        printf("ArgumentFormatter test: ");                argv = JS_PushArguments(jscontext, &mark, "s %ip s",                                 a_in, &nsITestXPCFoo2::GetIID(), foo, b_in);        if(argv)        {            nsISupports* fooc;            nsTestXPCFoo* foog;            if(JS_ConvertArguments(jscontext, 3, argv, "s %ip s",                                    &a_out, &fooc, &b_out))            {                if(fooc)                {                    if(NS_SUCCEEDED(fooc->QueryInterface(nsTestXPCFoo::GetIID(),                                     (void**)&foog)))                    {                        if(foog == foo)                        {                            if(!strcmp(a_in, a_out) && !strcmp(b_in, b_out))                                printf("passed\n");                            else                                printf(" conversion OK, but surrounding was mangled -- failed!\n");                        }                        else                            printf(" JS to native returned wrong value -- failed!\n");                        NS_RELEASE(foog);                    }                    else                    {                        printf(" could not QI value JS to native returned -- failed!\n");                    }                    NS_RELEASE(fooc);                }                else                {                    printf(" JS to native returned NULL -- failed!\n");                }            }            else            {                printf(" could not convert from JS to native -- failed!\n");            }            JS_PopArguments(jscontext, mark);        }        else        {            printf(" could not convert from native to JS -- failed!\n");        }    }    // cleanup    NS_RELEASE(foo);    if(glob)    {        JS_DeleteProperty(jscontext, glob, "foo");        JS_DeleteProperty(jscontext, glob, "echo");        JS_DeleteProperty(jscontext, glob, "bar");        JS_DeleteProperty(jscontext, glob, "foo2");        JS_DeleteProperty(jscontext, glob, "baz");        JS_DeleteProperty(jscontext, glob, "baz2");        JS_DeleteProperty(jscontext, glob, "receiver");        JS_SetGlobalObject(jscontext, JS_NewObject(jscontext, &global_class, NULL, NULL));    }    NS_RELEASE(wrapper);    NS_RELEASE(fool_wrapper);//    js_DumpGCHeap = stdout;//    JS_GC(jscontext);//    printf("-----------------------\n");//    JS_GC(jscontext);    // dump to log test...//    XPC_LOG_ALWAYS((""));//    XPC_LOG_ALWAYS(("after running JS_GC..."));//    XPC_LOG_ALWAYS((""));//    XPC_DUMP(xpc, 3);#endif//    xpc->DebugDump(3);//    printf("-----------------------\n");    xpc->AbandonJSContext(jscontext);    JS_DestroyContext(jscontext);    NS_RELEASE(xpc);    JS_DestroyRuntime(rt);    JS_ShutDown();    return 0;}