|
8 | 8 | #include "object.h"
|
9 | 9 | #include "path.h"
|
10 | 10 | #include "SandBox.h"
|
| 11 | +#include "Fiber.h" |
11 | 12 | #include "loaders.h"
|
12 | 13 | #include "ifs/util.h"
|
13 |
| - |
| 14 | +#include "Path.h" |
14 | 15 | namespace fibjs {
|
15 | 16 |
|
16 | 17 | static v8::MaybeLocal<v8::Module> resolveModule(
|
17 | 18 | v8::Local<v8::Context> context, v8::Local<v8::String> specifier,
|
18 | 19 | v8::Local<v8::FixedArray> import_assertions, v8::Local<v8::Module> referrer)
|
19 | 20 | {
|
| 21 | + Isolate* isolate = Isolate::current(context); |
| 22 | + |
| 23 | + for (int i = 0; i < import_assertions->Length(); i++) { |
| 24 | + v8::Local<v8::String> v8_assertion_key = import_assertions->Get(context, i).As<v8::String>(); |
| 25 | + puts(isolate->toString(v8_assertion_key).c_str()); |
| 26 | + } |
| 27 | + |
| 28 | + ThrowError(isolate->NewString("import is not supported in sandbox")); |
20 | 29 | return v8::MaybeLocal<v8::Module>();
|
21 | 30 | }
|
22 | 31 |
|
| 32 | +class async_import_data { |
| 33 | +public: |
| 34 | + obj_ptr<SandBox> m_sb; |
| 35 | + exlib::string id; |
| 36 | + exlib::string base; |
| 37 | + v8::Global<v8::Promise::Resolver> m_resolver; |
| 38 | +}; |
| 39 | + |
| 40 | +static result_t sync_import(async_import_data* data) |
| 41 | +{ |
| 42 | + JSFiber::EnterJsScope s; |
| 43 | + Isolate* isolate = data->m_sb->holder(); |
| 44 | + v8::Local<v8::Context> context = isolate->context(); |
| 45 | + |
| 46 | + v8::Local<v8::Value> retVal; |
| 47 | + puts(data->id.c_str()); |
| 48 | + puts(data->base.c_str()); |
| 49 | + result_t hr = data->m_sb->run_module(data->id, data->base, retVal); |
| 50 | + if (hr < 0) |
| 51 | + data->m_resolver.Get(isolate->m_isolate)->Reject(context, FillError(hr)); |
| 52 | + else |
| 53 | + data->m_resolver.Get(isolate->m_isolate)->Resolve(context, retVal); |
| 54 | + |
| 55 | + delete data; |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +v8::MaybeLocal<v8::Promise> SandBox::async_import(exlib::string id, exlib::string base) |
| 60 | +{ |
| 61 | + Isolate* isolate = holder(); |
| 62 | + v8::Local<v8::Context> context = isolate->context(); |
| 63 | + |
| 64 | + async_import_data* data = new async_import_data(); |
| 65 | + data->m_sb = this; |
| 66 | + data->id = id; |
| 67 | + os_dirname(base, data->base); |
| 68 | + |
| 69 | + v8::Local<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(context).FromMaybe(v8::Local<v8::Promise::Resolver>()); |
| 70 | + data->m_resolver.Reset(isolate->m_isolate, resolver); |
| 71 | + |
| 72 | + syncCall(isolate, sync_import, data); |
| 73 | + |
| 74 | + return resolver->GetPromise(); |
| 75 | +} |
| 76 | + |
| 77 | +v8::MaybeLocal<v8::Promise> SandBox::ImportModuleDynamically(v8::Local<v8::Context> context, |
| 78 | + v8::Local<v8::Data> host_defined_options, v8::Local<v8::Value> resource_name, |
| 79 | + v8::Local<v8::String> specifier, v8::Local<v8::FixedArray> import_assertions) |
| 80 | +{ |
| 81 | + Isolate* isolate = Isolate::current(context); |
| 82 | + v8::Local<v8::PrimitiveArray> _host_options = host_defined_options.As<v8::PrimitiveArray>(); |
| 83 | + uint32_t id = _host_options->Get(isolate->m_isolate, 0)->Uint32Value(context).FromMaybe(0); |
| 84 | + |
| 85 | + v8::String::Utf8Value _resource_name(isolate->m_isolate, resource_name); |
| 86 | + v8::String::Utf8Value _specifier(isolate->m_isolate, specifier); |
| 87 | + |
| 88 | + return isolate->m_sandboxes.find(id)->second->async_import(*_specifier, *_resource_name); |
| 89 | +} |
| 90 | + |
23 | 91 | result_t mjs_Loader::run(SandBox::Context* ctx, Buffer_base* src, exlib::string name,
|
24 | 92 | exlib::string arg_names, std::vector<v8::Local<v8::Value>>& args)
|
25 | 93 | {
|
@@ -48,12 +116,16 @@ result_t mjs_Loader::run(SandBox::Context* ctx, Buffer_base* src, exlib::string
|
48 | 116 | module->InstantiateModule(context, resolveModule).IsJust();
|
49 | 117 |
|
50 | 118 | v8::Local<v8::Value> promise = module->Evaluate(context).FromMaybe(v8::Local<v8::Value>());
|
51 |
| - if (promise.IsEmpty()) |
52 |
| - return throwSyntaxError(try_catch); |
| 119 | + if (promise.IsEmpty()) { |
| 120 | + try_catch.ReThrow(); |
| 121 | + return CALL_E_JAVASCRIPT; |
| 122 | + } |
53 | 123 |
|
54 | 124 | v8::Local<v8::Value> result = isolate->WaitPromise(promise);
|
55 |
| - if (result.IsEmpty()) |
56 |
| - return throwSyntaxError(try_catch); |
| 125 | + if (result.IsEmpty()) { |
| 126 | + try_catch.ReThrow(); |
| 127 | + return CALL_E_JAVASCRIPT; |
| 128 | + } |
57 | 129 |
|
58 | 130 | v8::Local<v8::Object>::Cast(args[2])->Set(context, isolate->NewString("exports"), module->GetModuleNamespace()).IsJust();
|
59 | 131 |
|
|
0 commit comments