本文整理汇总了C++中JS_GetRuntime函数的典型用法代码示例。如果您正苦于以下问题:C++ JS_GetRuntime函数的具体用法?C++ JS_GetRuntime怎么用?C++ JS_GetRuntime使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了JS_GetRuntime函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CreateGlobalObject
JSObject*
CreateGlobalObject(JSContext* cx, const JSClass* clasp, nsIPrincipal* principal,
JS::CompartmentOptions& aOptions)
{
MOZ_ASSERT(NS_IsMainThread(), "using a principal off the main thread?");
MOZ_ASSERT(principal);
MOZ_RELEASE_ASSERT(principal != nsContentUtils::GetNullSubjectPrincipal(),
"The null subject principal is getting inherited - fix that!");
RootedObject global(cx,
JS_NewGlobalObject(cx, clasp, nsJSPrincipals::get(principal),
JS::DontFireOnNewGlobalHook, aOptions));
if (!global)
return nullptr;
JSAutoCompartment ac(cx, global);
// The constructor automatically attaches the scope to the compartment private
// of |global|.
(void) new XPCWrappedNativeScope(cx, global);
if (clasp->flags & JSCLASS_DOM_GLOBAL) {
#ifdef DEBUG
// Verify that the right trace hook is called. Note that this doesn't
// work right for wrapped globals, since the tracing situation there is
// more complicated. Manual inspection shows that they do the right
// thing. Also note that we only check this for JSCLASS_DOM_GLOBAL
// classes because xpc::TraceXPCGlobal won't call
// TraceProtoAndIfaceCache unless that flag is set.
if (!((const js::Class*)clasp)->isWrappedNative())
{
VerifyTraceProtoAndIfaceCacheCalledTracer trc(JS_GetRuntime(cx));
TraceChildren(&trc, GCCellPtr(global.get()));
MOZ_ASSERT(trc.ok, "Trace hook on global needs to call TraceXPCGlobal for XPConnect compartments.");
}
#endif
const char* className = clasp->name;
AllocateProtoAndIfaceCache(global,
(strcmp(className, "Window") == 0 ||
strcmp(className, "ChromeWindow") == 0)
? ProtoAndIfaceCache::WindowLike
: ProtoAndIfaceCache::NonWindowLike);
}
return global;
}
示例2: ReportException
AutoJSAPI::~AutoJSAPI()
{
if (mOwnErrorReporting) {
ReportException();
// We need to do this _after_ processing the existing exception, because the
// JS engine can throw while doing that, and uses this bit to determine what
// to do in that case: squelch the exception if the bit is set, otherwise
// call the error reporter. Calling WarningOnlyErrorReporter with a
// non-warning will assert, so we need to make sure we do the former.
JS::ContextOptionsRef(cx()).setAutoJSAPIOwnsErrorReporting(mOldAutoJSAPIOwnsErrorReporting);
}
if (mOldErrorReporter.isSome()) {
JS_SetErrorReporter(JS_GetRuntime(cx()), mOldErrorReporter.value());
}
}
示例3: evalcx
static JSBool
evalcx(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
JSString *str;
JSObject *sandbox;
JSContext *subcx;
const jschar *src;
size_t srclen;
JSBool ret = JS_FALSE;
sandbox = NULL;
if(!JS_ConvertArguments(cx, argc, argv, "S / o", &str, &sandbox)) {
return JS_FALSE;
}
subcx = JS_NewContext(JS_GetRuntime(cx), 8L * 1024L);
if(!subcx) {
JS_ReportOutOfMemory(cx);
return JS_FALSE;
}
SETUP_REQUEST(subcx);
src = JS_GetStringChars(str);
srclen = JS_GetStringLength(str);
if(!sandbox) {
sandbox = JS_NewObject(subcx, NULL, NULL, NULL);
if(!sandbox || !JS_InitStandardClasses(subcx, sandbox)) {
goto done;
}
}
if(srclen == 0) {
*rval = OBJECT_TO_JSVAL(sandbox);
} else {
JS_EvaluateUCScript(subcx, sandbox, src, srclen, NULL, 0, rval);
}
ret = JS_TRUE;
done:
FINISH_REQUEST(subcx);
JS_DestroyContext(subcx);
return ret;
}
示例4: MOZ_ASSERT
void
AutoJSAPI::TakeOwnershipOfErrorReporting()
{
MOZ_ASSERT(!mOwnErrorReporting);
mOwnErrorReporting = true;
JSRuntime *rt = JS_GetRuntime(cx());
mOldAutoJSAPIOwnsErrorReporting = JS::ContextOptionsRef(cx()).autoJSAPIOwnsErrorReporting();
JS::ContextOptionsRef(cx()).setAutoJSAPIOwnsErrorReporting(true);
// Workers have their own error reporting mechanism which deals with warnings
// as well, so don't change the worker error reporter for now. Once we switch
// all of workers to TakeOwnershipOfErrorReporting(), we will just make the
// default worker error reporter assert that it only sees warnings.
if (mIsMainThread) {
JS_SetErrorReporter(rt, WarningOnlyErrorReporter);
}
}
示例5: nsPerformanceSnapshot
NS_IMETHODIMP nsPerformanceStatsService::GetSnapshot(JSContext* cx, nsIPerformanceSnapshot * *aSnapshot)
{
nsRefPtr<nsPerformanceSnapshot> snapshot = new nsPerformanceSnapshot();
nsresult rv = snapshot->Init(cx, mProcessId);
if (NS_FAILED(rv)) {
return rv;
}
js::GetPerfMonitoringTestCpuRescheduling(JS_GetRuntime(cx), &mProcessStayed, &mProcessMoved);
if (++mProcessUpdateCounter % 10 == 0) {
mozilla::unused << UpdateTelemetry();
}
snapshot.forget(aSnapshot);
return NS_OK;
}
示例6: to_array
static JSBool to_array(JSContext* js_context, JSObject* obj, uintN UNUSED(argc), jsval* UNUSED(argv), jsval* retval)
{
VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context);
JohnsonContext* context;
JohnsonRuntime* runtime;
Data_Get_Struct(ruby_context, JohnsonContext, context);
VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(JS_GetRuntime(js_context));
Data_Get_Struct(ruby_runtime, JohnsonRuntime, runtime);
PREPARE_JROOTS(js_context, 0);
VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL);
JCHECK(call_ruby_from_js(runtime, retval, self, rb_intern("to_a"), 0));
JRETURN;
}
示例7: importer_new_resolve
/*
* Like JSResolveOp, but flags provide contextual information as follows:
*
* JSRESOLVE_QUALIFIED a qualified property id: obj.id or obj[id], not id
* JSRESOLVE_ASSIGNING obj[id] is on the left-hand side of an assignment
* JSRESOLVE_DETECTING 'if (o.p)...' or similar detection opcode sequence
* JSRESOLVE_DECLARING var, const, or function prolog declaration opcode
* JSRESOLVE_CLASSNAME class name used when constructing
*
* The *objp out parameter, on success, should be null to indicate that id
* was not resolved; and non-null, referring to obj or one of its prototypes,
* if id was resolved.
*/
static JSBool
importer_new_resolve(JSContext *context,
JSObject **obj,
jsid *id,
unsigned flags,
JSObject **objp)
{
Importer *priv;
char *name;
JSBool ret = JS_TRUE;
jsid module_init_name;
*objp = NULL;
module_init_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
GJS_STRING_MODULE_INIT);
if (*id == module_init_name)
return JS_TRUE;
if (!gjs_get_string_id(context, *id, &name))
return JS_FALSE;
/* let Object.prototype resolve these */
if (strcmp(name, "valueOf") == 0 ||
strcmp(name, "toString") == 0 ||
strcmp(name, "__iterator__") == 0)
goto out;
priv = priv_from_js(context, *obj);
gjs_debug_jsprop(GJS_DEBUG_IMPORTER, "Resolve prop '%s' hook obj %p priv %p", name, *obj, priv);
if (priv == NULL) /* we are the prototype, or have the wrong class */
goto out;
JS_BeginRequest(context);
if (do_import(context, *obj, priv, name)) {
*objp = *obj;
} else {
ret = JS_FALSE;
}
JS_EndRequest(context);
out:
g_free(name);
return ret;
}
示例8: gjs_closure_new
GClosure*
gjs_closure_new(JSContext *context,
JSObject *callable,
const char *description,
gboolean root_function)
{
Closure *c;
c = (Closure*) g_closure_new_simple(sizeof(Closure), NULL);
c->runtime = JS_GetRuntime(context);
/* The saved context is used for lifetime management, so that the closure will
* be torn down with the context that created it. The context could be attached to
* the default context of the runtime using if we wanted the closure to survive
* the context that created it.
*/
c->context = context;
JS_BeginRequest(context);
c->obj = callable;
c->unref_on_global_object_finalized = FALSE;
GJS_INC_COUNTER(closure);
if (root_function) {
/* Fully manage closure lifetime if so asked */
gjs_keep_alive_add_global_child(context,
global_context_finalized,
c->obj,
c);
g_closure_add_invalidate_notifier(&c->base, NULL, closure_invalidated);
} else {
/* Only mark the closure as invalid if memory is managed
outside (i.e. by object.c for signals) */
g_closure_add_invalidate_notifier(&c->base, NULL, closure_set_invalid);
}
gjs_debug_closure("Create closure %p which calls object %p '%s'",
c, c->obj, description);
JS_EndRequest(context);
return &c->base;
}
示例9: gpsee_removeAsyncCallbackContext
/** Deletes all async callbacks associated with the current context. Suitable for use as as JSContextCallback.
* This is NOT SAFE to call from within an async callback.
* You must not call this function if you are not in the JSContext associated with the
* callback you are removing. This function is intended for being called during the finalization of a JSContext (ie.
* during the context callback, gpsee_contextCallback().)
*
* @note This call may traverse the entire linked list of registrations. Don't add and remove callbacks a lot.
*
* @param cx The state of the JS context if used as a JSContextCallback. If calling directly, pass JSCONTEXT_DESTROY.
* @param contextOp
* @returns JS_TRUE
*
* @todo Investigate using gpsee_removeAsyncCallbackContext() to clean up async callbacks on context shutdown.
*/
JSBool gpsee_removeAsyncCallbackContext(JSContext *cx, uintN contextOp)
{
gpsee_runtime_t *grt = (gpsee_runtime_t *) JS_GetRuntimePrivate(JS_GetRuntime(cx));
GPSEEAsyncCallback **cb, **cc, *freeme = NULL;
#ifdef GPSEE_DEBUG_BUILD
/* Assert that cx is on current thread */
JS_BeginRequest(cx);
JS_EndRequest(cx);
#endif
if (contextOp != JSCONTEXT_DESTROY)
return JS_TRUE;
if (!grt->asyncCallbacks)
return JS_TRUE;
/* Acquire mutex protecting grt->asyncCallbacks */
PR_Lock(grt->asyncCallbacks_lock);
/* Locate the first entry we want to remove */
for (cb = &grt->asyncCallbacks; *cb && (*cb)->cx != cx; cb = &(*cb)->next);
if (*cb)
{
freeme = *cb;
/* Locate the final entry we want remove */
for (cc = cb; *cc && (*cc)->cx == cx; cc = &(*cc)->next);
/* Remove all the entries we grabbed */
*cb = *cc;
}
/* Relinquish mutex */
PR_Unlock(grt->asyncCallbacks_lock);
/* Free the memory */
while (freeme)
{
GPSEEAsyncCallback *next = freeme->next;
JS_free(cx, freeme);
/* Break at end of removed segment */
if (&freeme->next == cc)
break;
freeme = next;
}
return JS_TRUE;
}
示例10: PromiseResolverTask
PromiseResolverTask(Promise* aPromise,
JS::Handle<JS::Value> aValue,
Promise::PromiseState aState)
: mPromise(aPromise)
, mValue(aValue)
, mState(aState)
{
MOZ_ASSERT(aPromise);
MOZ_ASSERT(mState != Promise::Pending);
MOZ_COUNT_CTOR(PromiseResolverTask);
JSContext* cx = nsContentUtils::GetSafeJSContext();
/* It's safe to use unsafeGet() here: the unsafeness comes from the
* possibility of updating the value of mJSObject without triggering the
* barriers. However if the value will always be marked, post barriers
* unnecessary. */
JS_AddNamedValueRootRT(JS_GetRuntime(cx), mValue.unsafeGet(),
"PromiseResolverTask.mValue");
}
示例11: JS_GetGlobalObject
static gpsee_realm_t *getRealm(JSContext *cx)
{
JSObject *global = JS_GetGlobalObject(cx);
gpsee_runtime_t *grt;
gpsee_realm_t *realm = NULL;
if ((realm = gpsee_getModuleScopeRealm(cx, NULL)))
return realm;
grt = JS_GetRuntimePrivate(JS_GetRuntime(cx));
gpsee_enterAutoMonitor(cx, &grt->monitors.realms);
if (grt && grt->realmsByContext)
realm = gpsee_ds_get(grt->realmsByContext, cx);
gpsee_leaveAutoMonitor(grt->monitors.realms);
if (global && JS_GET_CLASS(cx, global) == gpsee_getGlobalClass())
GPSEE_ASSERT(realm);
return realm;
}
示例12: jsj_WrapJSObject
/*
* The caller must call DeleteLocalRef() on the returned object when no more
* references remain.
*/
jobject
jsj_WrapJSObject(JSContext *cx, JNIEnv *jEnv, JSObject *js_obj)
{
jobject java_wrapper_obj;
JSObjectHandle *handle;
/* Create a tiny stub object to act as the GC root that points to the
JSObject from its netscape.javascript.JSObject counterpart. */
handle = (JSObjectHandle*)JS_malloc(cx, sizeof(JSObjectHandle));
if (!handle)
return NULL;
handle->js_obj = js_obj;
handle->rt = JS_GetRuntime(cx);
/* Create a new Java object that wraps the JavaScript object by storing its
address in a private integer field. */
#ifndef OJI
#if JS_BYTES_PER_LONG == 8
java_wrapper_obj =
(*jEnv)->NewObject(jEnv, njJSObject, njJSObject_JSObject, (jlong)handle);
#else
java_wrapper_obj =
(*jEnv)->NewObject(jEnv, njJSObject, njJSObject_JSObject, (jint)handle);
#endif
#else
if (JSJ_callbacks && JSJ_callbacks->get_java_wrapper != NULL) {
java_wrapper_obj = JSJ_callbacks->get_java_wrapper(jEnv, (jint)handle);
}
#endif /*! OJI */
if (!java_wrapper_obj) {
jsj_UnexpectedJavaError(cx, jEnv, "Couldn't create new instance of "
"netscape.javascript.JSObject");
goto done;
}
JS_AddNamedRoot(cx, &handle->js_obj, "&handle->js_obj");
done:
return java_wrapper_obj;
}
示例13: gpsee_operationCallback
/** Our "Operation Callback" multiplexes this Spidermonkey facility. It is called automatically by Spidermonkey, and is
* triggered on a regular interval by gpsee_asyncCallbackTriggerThreadFunc() */
JSBool gpsee_operationCallback(JSContext *cx)
{
gpsee_runtime_t *grt = (gpsee_runtime_t *) JS_GetRuntimePrivate(JS_GetRuntime(cx));
GPSEEAsyncCallback *cb;
/* The callbacks registered with GPSEE may want to invoke JSAPI functionality, which might toss us back out
* to another invocation of gpsee_operationCallback(). The JSAPI docs for "operation callbacks" [1] suggest
* removing the operation callback before calling JSAPI functionality from within an operation callback,
* then resetting it when we're done making JSAPI calls. Since it's rather inexpensive, we'll just do it here
* and then consumers of gpsee_addAsyncCallback() needn't worry about it (we don't want them touching that
* callback slot anyway!
*
* [1] https://developer.mozilla.org/en/JS_SetOperationCallback
*
* Another side note: we do it before if(cb) because if gpsee_asyncCallbacks is empty, we want to uninstall our
* operation callback altogether. */
JS_SetOperationCallback(cx, NULL);
cb = grt->asyncCallbacks;
if (cb)
{
GPSEEAsyncCallback *next;
do
{
/* Save the 'next' link in case the callback deletes itself */
next = cb->next;
/* Invoke callback */
if (!((*(cb->callback))(cb->cx, cb->userdata, cb)))
/* Propagate exceptions */
return JS_FALSE;
}
while ((cb = next));
/* Reinstall our operation callback */
JS_SetOperationCallback(cx, gpsee_operationCallback);
return JS_TRUE;
}
return JS_TRUE;
}
示例14: CheckFree
cl_mem dpoCContext::CreateBuffer(JSContext *cx, cl_mem_flags flags, size_t size, void *ptr, cl_int *err)
{
#ifdef INCREMENTAL_MEM_RELEASE
int freed;
cl_mem result;
bool didGC = false;
do {
freed = CheckFree();
result = clCreateBuffer(context, flags, size, ptr, err);
if ((*err != CL_OUT_OF_HOST_MEMORY) && (*err != CL_MEM_OBJECT_ALLOCATION_FAILURE) && (*err != CL_OUT_OF_RESOURCES)) break;
if (!freed && !didGC) {
JS_GC(JS_GetRuntime(cx));
didGC = freed = true;
}
} while (freed);
return result;
#else /* INCREMENTAL_MEM_RELEASE */
return clCreateBuffer(context, flags, size, ptr, err);
#endif /* INCREMENTAL_MEM_RELEASE */
}
示例15: jsd_JSDContextForJSContext
JSDContext*
jsd_JSDContextForJSContext(JSContext* context)
{
JSDContext* iter;
JSDContext* jsdc = NULL;
JSRuntime* runtime = JS_GetRuntime(context);
JSD_LOCK();
for( iter = (JSDContext*)_jsd_context_list.next;
iter != (JSDContext*)&_jsd_context_list;
iter = (JSDContext*)iter->links.next )
{
if( runtime == iter->jsrt )
{
jsdc = iter;
break;
}
}
JSD_UNLOCK();
return jsdc;
}