Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native support for loading ES modules #1440

Merged
merged 7 commits into from
Jan 3, 2019
Merged

Conversation

ry
Copy link
Member

@ry ry commented Jan 1, 2019

The intention is eventually to move TS into its own isolate, so that the runtime can be faster and leaner. This patch sets up the libdeno bindings for this to happen. A subsequent PR will move the compiler to a different isolate.

Ref #975

@ry ry requested a review from piscisaureus January 1, 2019 17:19
auto count = module_map_.count(filename);
if (count == 1) {
auto module = module_map_[filename].Get(isolate_);
resolve_module_.Reset(isolate_, module);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that there are chances where we might fetch a source multiple times, but discard all the fetched source code and only using the first fetched version?

(As a side question, does V8 InstantiateModule guarantees that the same module would not be running in the same context twice?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe (tho I'm not sure, I haven't tested it) that the module_map_ will properly prevent that from happening. The resolve_module_.Reset() here is only to pass around a reference during resolution.

@ry ry force-pushed the esm_libdeno branch 4 times, most recently from a066077 to 029ad3b Compare January 1, 2019 23:36
@ry ry force-pushed the esm_libdeno branch 2 times, most recently from 029ad3b to dc956c5 Compare January 2, 2019 17:59
ry added 4 commits January 2, 2019 13:15
For better greppability and conformance with other symbols in libdeno.rs
Introduces deno_execute_mod() for executing ES modules.
Seems to be necessary to get the previous commits to compile correctly.
# Build incrementally.
- $(DENO_BUILD_PATH)
# TODO Incremental build disabled because there are some bugs.
#- $(DENO_BUILD_PATH)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it super slow now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -64,7 +64,7 @@ before_script:
script:
- ./tools/lint.py
- ./tools/test_format.py
- ./tools/build.py -C target/release -j2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is based on this #1444 (explanation there)

v8::HandleScope handle_scope(isolate);
auto context = d->context_.Get(d->isolate_);
CHECK(!context.IsEmpty());
return deno::ExecuteMod(context, js_filename, js_source) ? 1 : 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this return value? Success/error? In c++ code I would expect to use bool, or alternatively you can do common c way of 0 (ok) / -1 (fail).

Copy link
Member Author

@ry ry Jan 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See docs in deno.h - this is to match the behavior of deno_execute 1 success / 0 fail.

(Yes I agree it should be the opposite, but let's do that in a separate PR.)

// A callback to implement ES Module imports. User must call deno_resolve_ok()
// at most once during deno_resolve_cb. If deno_resolve_ok() is not called, the
// specifier is considered invalid and will issue an error in JS. The reason
// deno_resolve_cb does not return deno_module is to avoid unnecessary heap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not return deno_module is to avoid unnecessary heap allocations

Did it seem very expensive? I doubt this is even in the vicinity of a hot path.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not so much about performance, but rather about keeping memory management simple. I don't want to have to think about allocating something in Rust and then passing ownership to libdeno

EXPECT_TRUE(deno_execute(d, nullptr, "a.js", "SnapshotBug()"));
deno_delete(d);
}

TEST(LibDenoTest, GlobalErrorHandling) {
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr});
Deno* d = deno_new(deno_config{0, snapshot, empty, nullptr, nullptr});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these nullptr additions....
Maybe we should do:

DenoConfig config;
config.set_snapshot("foo");
deno_new(config);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's getting a little annoying to update these tests all the time. I'm open to it, but note that this is a C api, so the above wouldn't work.

Copy link
Member

@piscisaureus piscisaureus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done!

@ry ry merged commit e809673 into denoland:master Jan 3, 2019
@ry ry mentioned this pull request Jan 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants