@@ -73,22 +73,64 @@ using v8::Array; |
73 | 73 | using v8::ArrayBuffer; |
74 | 74 | using v8::BigInt; |
75 | 75 | using v8::Context; |
| 76 | +using v8::Exception; |
76 | 77 | using v8::FunctionCallbackInfo; |
77 | 78 | using v8::FunctionTemplate; |
| 79 | +using v8::Integer; |
| 80 | +using v8::Isolate; |
78 | 81 | using v8::Local; |
| 82 | +using v8::MaybeLocal; |
79 | 83 | using v8::Object; |
80 | 84 | using v8::String; |
81 | 85 | using v8::Uint32; |
82 | 86 | using v8::Value; |
83 | 87 |
|
84 | 88 |
|
| 89 | +static MaybeLocal<Value> WASIException(Local<Context> context, |
| 90 | + int errorno, |
| 91 | + const char* syscall) { |
| 92 | + Isolate* isolate = context->GetIsolate(); |
| 93 | + Environment* env = Environment::GetCurrent(context); |
| 94 | + CHECK_NOT_NULL(env); |
| 95 | + const char* err_name = uvwasi_embedder_err_code_to_string(errorno); |
| 96 | + Local<String> js_code = OneByteString(isolate, err_name); |
| 97 | + Local<String> js_syscall = OneByteString(isolate, syscall); |
| 98 | + Local<String> js_msg = js_code; |
| 99 | + js_msg = |
| 100 | + String::Concat(isolate, js_msg, FIXED_ONE_BYTE_STRING(isolate, ", ")); |
| 101 | + js_msg = String::Concat(isolate, js_msg, js_syscall); |
| 102 | + Local<Object> e = |
| 103 | + Exception::Error(js_msg)->ToObject(context) |
| 104 | + .ToLocalChecked(); |
| 105 | + |
| 106 | + if (e->Set(context, |
| 107 | + env->errno_string(), |
| 108 | + Integer::New(isolate, errorno)).IsNothing() || |
| 109 | + e->Set(context, env->code_string(), js_code).IsNothing() || |
| 110 | + e->Set(context, env->syscall_string(), js_syscall).IsNothing()) { |
| 111 | + return MaybeLocal<Value>(); |
| 112 | + } |
| 113 | + |
| 114 | + return e; |
| 115 | +} |
| 116 | + |
| 117 | + |
85 | 118 | WASI::WASI(Environment* env, |
86 | 119 | Local<Object> object, |
87 | 120 | uvwasi_options_t* options) : BaseObject(env, object) { |
88 | 121 | MakeWeak(); |
89 | 122 | alloc_info_ = MakeAllocator(); |
90 | 123 | options->allocator = &alloc_info_; |
91 | | - CHECK_EQ(uvwasi_init(&uvw_, options), UVWASI_ESUCCESS); |
| 124 | + int err = uvwasi_init(&uvw_, options); |
| 125 | + if (err != UVWASI_ESUCCESS) { |
| 126 | + Local<Context> context = env->context(); |
| 127 | + MaybeLocal<Value> exception = WASIException(context, err, "uvwasi_init"); |
| 128 | + |
| 129 | + if (exception.IsEmpty()) |
| 130 | + return; |
| 131 | + |
| 132 | + context->GetIsolate()->ThrowException(exception.ToLocalChecked()); |
| 133 | + } |
92 | 134 | } |
93 | 135 |
|
94 | 136 |
|
|