Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
split ExceptionError function into two: ExceptionErrorWithLock and Ex…
Browse files Browse the repository at this point in the history
…ceptionError
  • Loading branch information
GustavoCaso committed Jan 10, 2022
1 parent bc67353 commit 6ea99a9
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ const char* CopyString(String::Utf8Value& value) {
}

RtnError ExceptionError(TryCatch& try_catch, Isolate* iso, Local<Context> ctx) {
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);

RtnError rtn = {nullptr, nullptr, nullptr};

if (try_catch.HasTerminated()) {
Expand Down Expand Up @@ -104,6 +100,16 @@ RtnError ExceptionError(TryCatch& try_catch, Isolate* iso, Local<Context> ctx) {
return rtn;
}

RtnError ExceptionErrorWithLock(TryCatch& try_catch,
Isolate* iso,
Local<Context> ctx) {
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);

return ExceptionError(try_catch, iso, ctx);
}

m_value* tracked_value(m_ctx* ctx, m_value* val) {
// (rogchap) we track values against a context so that when the context is
// closed (either manually or GC'd by Go) we can also release all the
Expand Down Expand Up @@ -299,12 +305,11 @@ RtnSnapshotBlob CreateSnapshot(const char* source,
Isolate* iso = creator.GetIsolate();
size_t index;
RtnSnapshotBlob rtn = {};
RtnError error = {nullptr, nullptr, nullptr};

{
HandleScope handle_scope(iso);
Local<Context> ctx = Context::New(iso);

TryCatch try_catch(iso);
Context::Scope context_scope(ctx);

MaybeLocal<String> maybeSrc =
Expand All @@ -316,20 +321,17 @@ RtnSnapshotBlob CreateSnapshot(const char* source,
ScriptOrigin script_origin(ogn);
Local<Script> script;
if (!Script::Compile(ctx, src, &script_origin).ToLocal(&script)) {
error.msg = CopyString("Couldn't compile snapshot script");
rtn.error = error;
rtn.error = ExceptionError(try_catch, iso, ctx);
return rtn;
}

Local<Value> result;
if (!script->Run(ctx).ToLocal(&result)) {
error.msg = CopyString("Couldn't run snapshot script");
rtn.error = error;
rtn.error = ExceptionError(try_catch, iso, ctx);
return rtn;
}
} else {
error.msg = CopyString("Couldn't parse source/origin string");
rtn.error = error;
rtn.error = ExceptionError(try_catch, iso, ctx);
return rtn;
}

Expand Down Expand Up @@ -542,7 +544,7 @@ RtnValue ObjectTemplateNewInstance(TemplatePtr ptr, ContextPtr ctx) {
Local<ObjectTemplate> obj_tmpl = tmpl.As<ObjectTemplate>();
Local<Object> obj;
if (!obj_tmpl->NewInstance(local_ctx).ToLocal(&obj)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}

Expand Down Expand Up @@ -639,7 +641,7 @@ RtnValue FunctionTemplateGetFunction(TemplatePtr ptr, ContextPtr ctx) {
RtnValue rtn = {};
Local<Function> fn;
if (!fn_tmpl->GetFunction(local_ctx).ToLocal(&fn)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}

Expand Down Expand Up @@ -743,19 +745,19 @@ RtnValue RunScript(ContextPtr ctx, const char* source, const char* origin) {
String::NewFromUtf8(iso, origin, NewStringType::kNormal);
Local<String> src, ogn;
if (!maybeSrc.ToLocal(&src) || !maybeOgn.ToLocal(&ogn)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}

ScriptOrigin script_origin(ogn);
Local<Script> script;
if (!Script::Compile(local_ctx, src, &script_origin).ToLocal(&script)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
Local<Value> result;
if (!script->Run(local_ctx).ToLocal(&result)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* val = new m_value;
Expand Down Expand Up @@ -804,7 +806,7 @@ RtnValue UnboundScriptRun(ContextPtr ctx, UnboundScriptPtr us_ptr) {
Local<Script> script = unbound_script->BindToCurrentContext();
Local<Value> result;
if (!script->Run(local_ctx).ToLocal(&result)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* val = new m_value;
Expand All @@ -822,12 +824,12 @@ RtnValue JSONParse(ContextPtr ctx, const char* str) {

Local<String> v8Str;
if (!String::NewFromUtf8(iso, str, NewStringType::kNormal).ToLocal(&v8Str)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
}

Local<Value> result;
if (!JSON::Parse(local_ctx, v8Str).ToLocal(&result)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* val = new m_value;
Expand Down Expand Up @@ -931,7 +933,7 @@ RtnValue NewValueString(IsolatePtr iso, const char* v) {
RtnValue rtn = {};
Local<String> str;
if (!String::NewFromUtf8(iso, v).ToLocal(&str)) {
rtn.error = ExceptionError(try_catch, iso, ctx->ptr.Get(iso));
rtn.error = ExceptionErrorWithLock(try_catch, iso, ctx->ptr.Get(iso));
return rtn;
}
m_value* val = new m_value;
Expand Down Expand Up @@ -1013,7 +1015,7 @@ RtnValue NewValueBigIntFromWords(IsolatePtr iso,
Local<BigInt> bigint;
if (!BigInt::NewFromWords(local_ctx, sign_bit, word_count, words)
.ToLocal(&bigint)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* val = new m_value;
Expand Down Expand Up @@ -1061,7 +1063,7 @@ RtnString ValueToDetailString(ValuePtr ptr) {
RtnString rtn = {0};
Local<String> str;
if (!value->ToDetailString(local_ctx).ToLocal(&str)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
String::Utf8Value ds(iso, str);
Expand Down Expand Up @@ -1104,7 +1106,7 @@ RtnValue ValueToObject(ValuePtr ptr) {
RtnValue rtn = {};
Local<Object> obj;
if (!value->ToObject(local_ctx).ToLocal(&obj)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* new_val = new m_value;
Expand Down Expand Up @@ -1437,12 +1439,12 @@ RtnValue ObjectGet(ValuePtr ptr, const char* key) {
Local<String> key_val;
if (!String::NewFromUtf8(iso, key, NewStringType::kNormal)
.ToLocal(&key_val)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
Local<Value> result;
if (!obj->Get(local_ctx, key_val).ToLocal(&result)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* new_val = new m_value;
Expand Down Expand Up @@ -1479,7 +1481,7 @@ RtnValue ObjectGetIdx(ValuePtr ptr, uint32_t idx) {

Local<Value> result;
if (!obj->Get(local_ctx, idx).ToLocal(&result)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* new_val = new m_value;
Expand Down Expand Up @@ -1523,7 +1525,7 @@ RtnValue NewPromiseResolver(ContextPtr ctx) {
RtnValue rtn = {};
Local<Promise::Resolver> resolver;
if (!Promise::Resolver::New(local_ctx).ToLocal(&resolver)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* val = new m_value;
Expand Down Expand Up @@ -1572,12 +1574,12 @@ RtnValue PromiseThen(ValuePtr ptr, int callback_ref) {
Local<Function> func;
if (!Function::New(local_ctx, FunctionTemplateCallback, cbData)
.ToLocal(&func)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
Local<Promise> result;
if (!promise->Then(local_ctx, func).ToLocal(&result)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* result_val = new m_value;
Expand All @@ -1597,20 +1599,20 @@ RtnValue PromiseThen2(ValuePtr ptr, int on_fulfilled_ref, int on_rejected_ref) {
Local<Function> onFulfilledFunc;
if (!Function::New(local_ctx, FunctionTemplateCallback, onFulfilledData)
.ToLocal(&onFulfilledFunc)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
Local<Integer> onRejectedData = Integer::New(iso, on_rejected_ref);
Local<Function> onRejectedFunc;
if (!Function::New(local_ctx, FunctionTemplateCallback, onRejectedData)
.ToLocal(&onRejectedFunc)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
Local<Promise> result;
if (!promise->Then(local_ctx, onFulfilledFunc, onRejectedFunc)
.ToLocal(&result)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* result_val = new m_value;
Expand All @@ -1630,12 +1632,12 @@ RtnValue PromiseCatch(ValuePtr ptr, int callback_ref) {
Local<Function> func;
if (!Function::New(local_ctx, FunctionTemplateCallback, cbData)
.ToLocal(&func)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
Local<Promise> result;
if (!promise->Catch(local_ctx, func).ToLocal(&result)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* result_val = new m_value;
Expand Down Expand Up @@ -1682,7 +1684,7 @@ RtnValue FunctionCall(ValuePtr ptr, ValuePtr recv, int argc, ValuePtr args[]) {

Local<Value> result;
if (!fn->Call(local_ctx, local_recv, argc, argv).ToLocal(&result)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* rtnval = new m_value;
Expand All @@ -1701,7 +1703,7 @@ RtnValue FunctionNewInstance(ValuePtr ptr, int argc, ValuePtr args[]) {
buildCallArguments(iso, argv, argc, args);
Local<Object> result;
if (!fn->NewInstance(local_ctx, argc, argv).ToLocal(&result)) {
rtn.error = ExceptionError(try_catch, iso, local_ctx);
rtn.error = ExceptionErrorWithLock(try_catch, iso, local_ctx);
return rtn;
}
m_value* rtnval = new m_value;
Expand Down

0 comments on commit 6ea99a9

Please sign in to comment.