-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
213 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
@0x96c290dbf479ac0c; | ||
|
||
const pythonEntrypoint :Text = embed "generated/python-entrypoint.js"; | ||
const pyodidePackagesTar :Data = embed "generated/pyodide_packages.tar"; | ||
const pyodideLock :Text = embed "generated/pyodide-lock.json"; | ||
const pythonEntrypoint :Text = embed "python-entrypoint.js"; | ||
const pyodidePackagesTar :Data = embed "pyodide_packages.tar"; | ||
const pyodideLock :Text = embed "pyodide-lock.json"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "setup-emscripten.h" | ||
|
||
#include <pyodide/generated/emscripten_setup.capnp.h> | ||
|
||
namespace workerd::api::pyodide { | ||
|
||
jsg::JsValue SetupEmscripten::getModule(jsg::Lock& js) { | ||
jsg::JsValue jsval(pyodide::initializeEmscriptenRuntime(js)); | ||
module_ = jsval.addRef(js); | ||
return KJ_ASSERT_NONNULL(module_).getHandle(js); | ||
} | ||
|
||
v8::Local<v8::Value> initializeEmscriptenRuntime(jsg::Lock& js) { | ||
js.setAllowEval(true); | ||
KJ_DEFER(js.setAllowEval(false)); | ||
|
||
v8::Local<v8::String> contentStr = | ||
jsg::v8Str(js.v8Isolate, EMSCRIPTEN_SETUP->getCode().asArray()); | ||
v8::ScriptOrigin origin( | ||
jsg::v8StrIntern(js.v8Isolate, "pyodide-internal:generated/emscriptenSetup"), 0, 0, false, -1, | ||
v8::Local<v8::Value>(), false, false, true); | ||
v8::ScriptCompiler::Source source(contentStr, origin); | ||
|
||
v8::Local<v8::Module> module; | ||
if (!v8::ScriptCompiler::CompileModule(js.v8Isolate, &source).ToLocal(&module)) { | ||
KJ_FAIL_ASSERT("Emscripten Setup code didn't parse"); | ||
} | ||
// instantiateEmscriptenModule2(js, module); | ||
jsg::instantiateModule(js, module); | ||
|
||
auto handle = jsg::check(module->Evaluate(js.v8Context())); | ||
KJ_ASSERT(handle->IsPromise()); | ||
auto prom = handle.As<v8::Promise>(); | ||
KJ_ASSERT(prom->State() != v8::Promise::PromiseState::kPending); | ||
KJ_ASSERT(module->GetStatus() != v8::Module::kErrored); | ||
auto result = | ||
js.v8Get(module->GetModuleNamespace().As<v8::Object>(), "instantiateEmscriptenModule"_kj); | ||
|
||
auto src = EMSCRIPTEN_SETUP->getPyodideAsmWasm(); | ||
auto wasmModule = jsg::check(v8::WasmModuleObject::Compile( | ||
js.v8Isolate, v8::MemorySpan<const uint8_t>(src.begin(), src.size()))); | ||
auto arg2Buffer = | ||
v8::ArrayBuffer::New(js.v8Isolate, EMSCRIPTEN_SETUP->getPythonStdlibZip().size(), | ||
v8::BackingStoreInitializationMode::kUninitialized); | ||
memcpy(arg2Buffer->Data(), EMSCRIPTEN_SETUP->getPythonStdlibZip().begin(), | ||
EMSCRIPTEN_SETUP->getPythonStdlibZip().size()); | ||
|
||
v8::LocalVector<v8::Value> argv(js.v8Isolate, 3); | ||
argv[0] = v8::Boolean::New(js.v8Isolate, true); | ||
argv[1] = kj::mv(arg2Buffer); | ||
argv[2] = kj::mv(wasmModule); | ||
auto funcres = jsg::check( | ||
result.As<v8::Function>()->Call(js.v8Context(), js.v8Null(), argv.size(), argv.data())); | ||
KJ_ASSERT(funcres->IsPromise()); | ||
auto promise = funcres.As<v8::Promise>(); | ||
if (promise->State() == v8::Promise::PromiseState::kPending) { | ||
js.runMicrotasks(); | ||
} | ||
KJ_ASSERT(promise->State() == v8::Promise::PromiseState::kFulfilled); | ||
auto promresult = promise->Result(); | ||
return promresult; | ||
} | ||
} // namespace workerd::api::pyodide |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#pragma once | ||
|
||
#include <workerd/io/io-context.h> | ||
#include <workerd/jsg/jsg.h> | ||
#include <workerd/jsg/modules-new.h> | ||
#include <workerd/jsg/url.h> | ||
|
||
namespace workerd::api::pyodide { | ||
|
||
class EmscriptenModule: public jsg::Object { | ||
public: | ||
explicit EmscriptenModule(jsg::Lock& js) {}; | ||
JSG_STRUCT(EmscriptenModule); | ||
|
||
private: | ||
}; | ||
|
||
using instantiateEmscriptenModuleFunction = jsg::Function<jsg::Promise<jsg::JsRef<jsg::JsValue>>( | ||
jsg::JsBoolean, jsg::JsString, jsg::JsString)>; | ||
|
||
class SetupEmscripten: public jsg::Object { | ||
public: | ||
SetupEmscripten() {}; | ||
SetupEmscripten(jsg::Lock& js, const jsg::Url&) {} | ||
|
||
jsg::JsValue getModule(jsg::Lock& js); | ||
|
||
JSG_RESOURCE_TYPE(SetupEmscripten) { | ||
JSG_METHOD(getModule); | ||
} | ||
|
||
private: | ||
kj::Maybe<jsg::JsRef<jsg::JsValue>> module_; | ||
}; | ||
|
||
#define EW_SETUP_EMSCRIPTEN_ISOLATE_TYPES api::pyodide::SetupEmscripten | ||
|
||
template <class Registry> | ||
void registerSetupEmscriptenModule(Registry& registry, auto featureFlags) { | ||
registry.template addBuiltinModule<SetupEmscripten>( | ||
"internal:setup-emscripten", workerd::jsg::ModuleRegistry::Type::INTERNAL); | ||
} | ||
|
||
template <typename TypeWrapper> | ||
kj::Own<jsg::modules::ModuleBundle> getInternalSetupEmscriptenModuleBundle(auto featureFlags) { | ||
jsg::modules::ModuleBundle::BuiltinBuilder builder( | ||
jsg::modules::ModuleBundle::BuiltinBuilder::Type::BUILTIN_ONLY); | ||
static const auto kSpecifier = "internal:setup-emscripten"_url; | ||
builder.addObject<SetupEmscripten, TypeWrapper>(kSpecifier); | ||
return builder.finish(); | ||
} | ||
|
||
v8::Local<v8::Value> initializeEmscriptenRuntime(jsg::Lock&); | ||
|
||
} // namespace workerd::api::pyodide |
Oops, something went wrong.