diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-01-09 12:59:46 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-09 12:59:46 -0500 |
commit | 0ceb554343ff3d311a19f027b7aa8f0806bbb162 (patch) | |
tree | 24603f165b7da831c2b68ad9472e26fabf72134c /src/isolate.rs | |
parent | 3afdae165d3bc9acb5e5a4749334d2673f2566af (diff) |
Native ES modules (#1460)
* Native ES modules
This is a major refactor of internal compiler.
Before: JS and TS both were sent through the typescript compiler where
their imports were parsed and handled. Both compiled to AMD JS and
finally sent to V8
Now: JS is sent directly into V8. TS is sent through the typescript
compiler, but tsc generates ES modules now instead of AMD. This
generated JS is then dumped into V8.
This should much faster for pure JS code. It may improve TS compilation
speed.
In the future this allows us to separate TS out of the runtime heap and
into its own dedicated snapshot. This will result in a smaller runtime
heap, and thus should be faster.
Some tests were unfortunately disabled to ease landing this patch:
1. compiler_tests.ts which I intend to bring back in later commits.
2. Some text_encoding_test.ts tests which made the file invalid utf8.
See PR for a discussion.
Also worth noting that this is necessary to support WASM
Diffstat (limited to 'src/isolate.rs')
-rw-r--r-- | src/isolate.rs | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/src/isolate.rs b/src/isolate.rs index c4174de3f..fcd07d23e 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -1,5 +1,4 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. - // Do not use FlatBuffers in this module. // TODO Currently this module uses Tokio, but it would be nice if they were // decoupled. @@ -253,13 +252,14 @@ impl Isolate { /// Executes the provided JavaScript module. pub fn execute_mod(&self, js_filename: &str) -> Result<(), JSError> { - let out = self.state.dir.code_fetch(js_filename, ".").unwrap(); - debug!("module_resolve complete {}", out.filename); + let out = + code_fetch_and_maybe_compile(&self.state, js_filename, ".").unwrap(); - let filename = CString::new(js_filename).unwrap(); + let filename = CString::new(out.filename.clone()).unwrap(); let filename_ptr = filename.as_ptr() as *const i8; let js_source = CString::new(out.js_source().clone()).unwrap(); + let js_source = CString::new(js_source).unwrap(); let js_source_ptr = js_source.as_ptr() as *const i8; let r = unsafe { @@ -364,6 +364,25 @@ impl Drop for Isolate { } } +use compiler::compile_sync; +use compiler::CodeFetchOutput; +use msg; +fn code_fetch_and_maybe_compile( + state: &Arc<IsolateState>, + specifier: &str, + referrer: &str, +) -> Result<CodeFetchOutput, DenoError> { + let mut out = state.dir.code_fetch(specifier, referrer)?; + if out.media_type == msg::MediaType::TypeScript + && out.maybe_output_code.is_none() + { + debug!(">>>>> compile_sync START"); + out = compile_sync(state, specifier, &referrer).unwrap(); + debug!(">>>>> compile_sync END"); + } + Ok(out) +} + extern "C" fn resolve_cb( user_data: *mut c_void, specifier_ptr: *const c_char, @@ -378,16 +397,21 @@ extern "C" fn resolve_cb( debug!("module_resolve callback {} {}", specifier, referrer); let isolate = unsafe { Isolate::from_raw_ptr(user_data) }; - let out = isolate.state.dir.code_fetch(specifier, referrer).unwrap(); - debug!("module_resolve complete {}", out.filename); + let out = + code_fetch_and_maybe_compile(&isolate.state, specifier, referrer).unwrap(); + + let filename = CString::new(out.filename.clone()).unwrap(); + let filename_ptr = filename.as_ptr() as *const i8; - // TODO js_source is not null terminated, therefore the clone. let js_source = CString::new(out.js_source().clone()).unwrap(); - let filename = out.filename.as_ptr() as *const i8; let js_source_ptr = js_source.as_ptr() as *const i8; unsafe { - libdeno::deno_resolve_ok(isolate.libdeno_isolate, filename, js_source_ptr) + libdeno::deno_resolve_ok( + isolate.libdeno_isolate, + filename_ptr, + js_source_ptr, + ) }; } |