本文整理汇总了C++中Persistent::DetachGlobal方法的典型用法代码示例。如果您正苦于以下问题:C++ Persistent::DetachGlobal方法的具体用法?C++ Persistent::DetachGlobal怎么用?C++ Persistent::DetachGlobal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Persistent
的用法示例。
在下文中一共展示了Persistent::DetachGlobal方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ThrowException
//.........这里部分代码省略.........
// New and user context share code. DRY it up.
if (context_flag == userContext || context_flag == newContext) {
// Enter the context
context->Enter();
// Call the initCallback, if it exists
if (nContext) {
Persistent<Function> initCallback = nContext->GetInitCallback();
if (!initCallback.IsEmpty()) {
Handle<Value> callbackArgs[] = { contextArg, context->Global() };
initCallback->Call(contextArg, 2, callbackArgs);
}
}
// Copy everything from the passed in sandbox (either the persistent
// context for runInContext(), or the sandbox arg to runInNewContext()).
keys = sandbox->GetPropertyNames();
for (i = 0; i < keys->Length(); i++) {
Handle<String> key = keys->Get(Integer::New(i))->ToString();
Handle<Value> value = sandbox->Get(key);
if (value == sandbox) {
value = context->Global();
}
context->Global()->Set(key, value);
}
}
Handle<Value> result;
Handle<Script> script;
if (input_flag == compileCode) {
// well, here WrappedScript::New would suffice in all cases, but maybe
// Compile has a little better performance where possible
script = output_flag == returnResult ? Script::Compile(code, filename) : Script::New(code, filename);
if (script.IsEmpty()) {
// Hack because I can't get a proper stacktrace on SyntaxError
return Undefined();
}
} else {
WrappedScript *n_script = NativeObject::Unwrap<WrappedScript>(args.Holder());
if (!n_script) {
return ThrowException(Exception::Error(String::New("Must be called as a method of Script.")));
} else if (n_script->script_.IsEmpty()) {
return ThrowException(Exception::Error(String::New("'this' must be a result of previous "
"new Script(code) call.")));
}
script = n_script->script_;
}
if (output_flag == returnResult) {
result = script->Run();
if (result.IsEmpty()) {
if (context_flag == newContext) {
context->DetachGlobal();
context->Exit();
context.Dispose();
}
return Undefined();
}
} else {
WrappedScript *n_script = NativeObject::Unwrap<WrappedScript>(args.Holder());
if (!n_script) {
return ThrowException(Exception::Error(String::New("Must be called as a method of Script.")));
}
n_script->script_ = Persistent<Script>::New(script);
result = args.This();
}
if (context_flag == userContext || context_flag == newContext) {
// success! copy changes back onto the sandbox object.
keys = context->Global()->GetPropertyNames();
for (i = 0; i < keys->Length(); i++) {
Handle<String> key = keys->Get(Integer::New(i))->ToString();
Handle<Value> value = context->Global()->Get(key);
if (value == context->Global()) {
value = sandbox;
}
sandbox->Set(key, value);
}
}
if (context_flag == newContext) {
// Clean up, clean up, everybody everywhere!
context->DetachGlobal();
context->Exit();
context.Dispose();
} else if (context_flag == userContext) {
// Exit the passed in context.
context->Exit();
}
if (result->IsObject()) {
Local<Context> creation = result->ToObject()->CreationContext();
}
return result == args.This() ? result : scope.Close(result);
}
示例2: ThrowException
Handle<Value> WrappedScript::EvalMachine(const Arguments& args)
{
HandleScope scope;
if (input_flag == compileCode && args.Length() < 1) {
return ThrowException(Exception::TypeError(String::New("needs at least 'code' argument.")));
}
const int sandbox_index = input_flag == compileCode ? 1 : 0;
if (context_flag == userContext && args.Length() < (sandbox_index + 1)) {
return ThrowException(Exception::TypeError(String::New("needs a 'context' argument.")));
}
Local<String> code;
if (input_flag == compileCode) code = args[0]->ToString();
Local<Object> sandbox;
if (context_flag == newContext) {
sandbox = args[sandbox_index]->IsObject() ? args[sandbox_index]->ToObject() : Object::New();
} else if (context_flag == userContext) {
sandbox = args[sandbox_index]->ToObject();
}
int filename_offset = 1;
if (context_flag == thisContext) {
filename_offset = 0;
}
const int filename_index = sandbox_index + filename_offset;
Local<String> filename =
args.Length() > filename_index ? args[filename_index]->ToString() : String::New("evalmachine.<anonymous>");
const int display_error_index = args.Length() - 1;
bool display_error = false;
if (args.Length() > display_error_index && args[display_error_index]->IsBoolean()
&& args[display_error_index]->BooleanValue() == true) {
display_error = true;
}
Persistent<Context> context;
Local<Array> keys;
unsigned int i;
WrappedContext *nContext = NULL;
Local<Object> contextArg;
if (context_flag == newContext) {
// Create the new context
context = Context::New();
} else if (context_flag == userContext) {
// Use the passed in context
contextArg = args[sandbox_index]->ToObject();
nContext = WrappedContext::Unwrap(contextArg);
context = nContext->GetV8Context();
}
// New and user context share code. DRY it up.
if (context_flag == userContext || context_flag == newContext) {
// Enter the context
context->Enter();
}
Handle<Value> result;
Handle<Script> script;
if (input_flag == compileCode) {
// well, here WrappedScript::New would suffice in all cases, but maybe
// Compile has a little better performance where possible
script = output_flag == returnResult ? Script::Compile(code, filename) : Script::New(code, filename);
if (script.IsEmpty()) {
// Hack because I can't get a proper stacktrace on SyntaxError
return Undefined();
}
} else {
WrappedScript *n_script = NativeObject::Unwrap<WrappedScript>(args.Holder());
if (!n_script) {
return ThrowException(Exception::Error(String::New("Must be called as a method of Script.")));
} else if (n_script->script_.IsEmpty()) {
return ThrowException(Exception::Error(String::New("'this' must be a result of previous "
"new Script(code) call.")));
}
script = n_script->script_;
}
if (output_flag == returnResult) {
result = script->Run();
if (result.IsEmpty()) {
if (context_flag == newContext) {
context->DetachGlobal();
context->Exit();
context.Dispose();
}
return Undefined();
}
} else {
WrappedScript *n_script = NativeObject::Unwrap<WrappedScript>(args.Holder());
if (!n_script) {
return ThrowException(Exception::Error(String::New("Must be called as a method of Script.")));
//.........这里部分代码省略.........