xpc->InitJSContext(cx, glob);
xpc->InitJSContext(jscontext, glob);
int main(){    JSRuntime *rt;    JSContext *cx;    JSObject *glob;    gErrFile = stderr;    gOutFile = stdout;    RegAllocator();    rt = JS_NewRuntime(8L * 1024L * 1024L);    if (!rt)        return 1;    cx = JS_NewContext(rt, 8192);    if (!cx)        return 1;    JS_SetErrorReporter(cx, my_ErrorReporter);    nsIXPConnect* xpc = XPC_GetXPConnect();    if(!xpc)    {        printf("XPC_GetXPConnect() returned NULL!\n");        return 1;    }/*    // old code where global object was plain object    glob = JS_NewObject(cx, &global_class, NULL, NULL);    if (!glob)        return 1;    if (!JS_InitStandardClasses(cx, glob))        return 1;    if (!JS_DefineFunctions(cx, glob, glob_functions))        return 1;    xpc->InitJSContext(cx, glob);*/    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* fool_wrapper = NULL;/*    if(NS_SUCCEEDED(xpc->WrapNative(cx, foo, nsITestXPCFoo::IID(), &wrapper)))*/    // new code where global object is a wrapped xpcom object    if(NS_SUCCEEDED(xpc->InitJSContextWithNewWrappedGlobal(                                    cx, foo, nsITestXPCFoo::IID(), &wrapper)))    {        wrapper->GetJSObject(&glob);        JS_DefineFunctions(cx, glob, glob_functions);        nsTestXPCFoo* fool = new nsTestXPCFoo();        xpc->WrapNative(cx, fool, nsITestXPCFoo2::IID(), &fool_wrapper);        if(NS_SUCCEEDED(xpc->WrapNative(cx, foo, nsITestXPCFoo2::IID(), &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(cx, 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(cx, myEcho, nsIEcho::IID(), &echo_wrapper);            echo_wrapper->GetJSObject(&echo_jsobj);            echo_jsval = OBJECT_TO_JSVAL(echo_jsobj);            JS_SetProperty(cx, glob, "echo", &echo_jsval);            char* txt[] = {              "load('testxpc.js');",              0,            };            for(char** p = txt; *p; p++)                JS_EvaluateScript(cx, glob, *p, strlen(*p), "builtin", 1, &rval);            if(JS_GetProperty(cx, glob, "bar", &v) && JSVAL_IS_OBJECT(v))            {                JSObject* bar = JSVAL_TO_OBJECT(v);                nsIXPConnectWrappedJS* wrapper3;                if(NS_SUCCEEDED(xpc->WrapJS(cx,                                       JSVAL_TO_OBJECT(v),                                       nsITestXPCFoo::IID(), &wrapper3)))                {                    nsITestXPCFoo* ptr = (nsITestXPCFoo*)wrapper3;                    int result;                    JSObject* test_js_obj;                    ptr->Test(11, 13, &result);                    printf("call to ptr->Test returned %d\n", result);                        nsIXPConnectWrappedJSMethods* methods;                    wrapper3->QueryInterface(nsIXPConnectWrappedJSMethods::IID(),                                             (void**) &methods);                    methods->GetJSObject(&test_js_obj);                    printf("call to methods->GetJSObject() returned: %s\n",                             test_js_obj == JSVAL_TO_OBJECT(v) ?                            "expected result" : "WRONG RESULT" );                    // 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);    }    NS_RELEASE(foo);    // 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);    if(glob)    {        JS_DeleteProperty(cx, glob, "foo");        JS_DeleteProperty(cx, glob, "echo");        JS_DeleteProperty(cx, glob, "bar");        JS_DeleteProperty(cx, glob, "foo2");        JS_DeleteProperty(cx, glob, "baz");        JS_DeleteProperty(cx, glob, "baz2");        JS_DeleteProperty(cx, glob, "reciever");        JS_SetGlobalObject(cx, JS_NewObject(cx, &global_class, NULL, NULL));    }    NS_RELEASE(wrapper);    NS_RELEASE(fool_wrapper);//    js_DumpGCHeap = stdout;    JS_GC(cx);//    printf("-----------------------\n");//    JS_GC(cx);    // dump to log test...    XPC_LOG_ALWAYS((""));    XPC_LOG_ALWAYS(("after running JS_GC..."));    XPC_LOG_ALWAYS((""));    XPC_DUMP(xpc, 3);    NS_RELEASE(xpc);    JS_DestroyContext(cx);    JS_DestroyRuntime(rt);    JS_ShutDown();    return 0;}