2020-01-02 13:57:00 -05:00
|
|
|
// Copyright 2019-2020 the Deno authors. All rights reserved. MIT license.
|
2019-12-20 14:54:20 -05:00
|
|
|
//! For compiling scripts.
|
2020-02-11 17:01:27 -05:00
|
|
|
use crate::InIsolate;
|
2019-12-20 14:54:20 -05:00
|
|
|
use crate::Isolate;
|
|
|
|
use crate::Local;
|
|
|
|
use crate::Module;
|
|
|
|
use crate::ScriptOrigin;
|
|
|
|
use crate::String;
|
|
|
|
use std::mem::MaybeUninit;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
fn v8__ScriptCompiler__Source__CONSTRUCT(
|
|
|
|
buf: &mut MaybeUninit<Source>,
|
|
|
|
source_string: &String,
|
|
|
|
origin: &ScriptOrigin,
|
|
|
|
);
|
|
|
|
fn v8__ScriptCompiler__Source__DESTRUCT(this: &mut Source);
|
|
|
|
|
|
|
|
fn v8__ScriptCompiler__CompileModule(
|
|
|
|
isoate: &Isolate,
|
|
|
|
source: &Source,
|
|
|
|
options: CompileOptions,
|
|
|
|
no_cache_reason: NoCacheReason,
|
|
|
|
) -> *mut Module;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
/// Source code which can be then compiled to a UnboundScript or Script.
|
|
|
|
pub struct Source([usize; 8]);
|
|
|
|
|
|
|
|
impl Source {
|
|
|
|
// TODO(ry) cached_data
|
|
|
|
pub fn new(source_string: Local<String>, origin: &ScriptOrigin) -> Self {
|
|
|
|
let mut buf = MaybeUninit::<Self>::uninit();
|
|
|
|
unsafe {
|
|
|
|
v8__ScriptCompiler__Source__CONSTRUCT(&mut buf, &source_string, origin);
|
|
|
|
buf.assume_init()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Source {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { v8__ScriptCompiler__Source__DESTRUCT(self) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub enum CompileOptions {
|
|
|
|
NoCompileOptions = 0,
|
|
|
|
ConsumeCodeCache,
|
|
|
|
EagerCompile,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The reason for which we are not requesting or providing a code cache.
|
|
|
|
#[repr(C)]
|
|
|
|
pub enum NoCacheReason {
|
|
|
|
NoReason = 0,
|
|
|
|
BecauseCachingDisabled,
|
|
|
|
BecauseNoResource,
|
|
|
|
BecauseInlineScript,
|
|
|
|
BecauseModule,
|
|
|
|
BecauseStreamingSource,
|
|
|
|
BecauseInspector,
|
|
|
|
BecauseScriptTooSmall,
|
|
|
|
BecauseCacheTooCold,
|
|
|
|
BecauseV8Extension,
|
|
|
|
BecauseExtensionModule,
|
|
|
|
BecausePacScript,
|
|
|
|
BecauseInDocumentWrite,
|
|
|
|
BecauseResourceWithNoCacheHandler,
|
|
|
|
BecauseDeferredProduceCodeCache,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compile an ES module, returning a Module that encapsulates the compiled
|
|
|
|
/// code.
|
|
|
|
///
|
|
|
|
/// Corresponds to the ParseModule abstract operation in the ECMAScript
|
|
|
|
/// specification.
|
2019-12-24 18:31:36 -05:00
|
|
|
pub fn compile_module<'a>(
|
2020-02-11 17:01:27 -05:00
|
|
|
scope: &mut impl InIsolate,
|
2019-12-20 14:54:20 -05:00
|
|
|
source: Source,
|
2019-12-24 18:31:36 -05:00
|
|
|
) -> Option<Local<'a, Module>> {
|
2019-12-23 20:23:55 -05:00
|
|
|
compile_module2(
|
2020-02-11 17:01:27 -05:00
|
|
|
scope,
|
2019-12-23 20:23:55 -05:00
|
|
|
source,
|
|
|
|
CompileOptions::NoCompileOptions,
|
|
|
|
NoCacheReason::NoReason,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as compile_module with more options.
|
2019-12-24 18:31:36 -05:00
|
|
|
pub fn compile_module2<'a>(
|
2020-02-11 17:01:27 -05:00
|
|
|
scope: &mut impl InIsolate,
|
2019-12-23 20:23:55 -05:00
|
|
|
source: Source,
|
2019-12-20 14:54:20 -05:00
|
|
|
options: CompileOptions,
|
|
|
|
no_cache_reason: NoCacheReason,
|
2019-12-24 18:31:36 -05:00
|
|
|
) -> Option<Local<'a, Module>> {
|
2019-12-20 14:54:20 -05:00
|
|
|
unsafe {
|
2019-12-25 06:39:42 -05:00
|
|
|
Local::from_raw(v8__ScriptCompiler__CompileModule(
|
2020-02-11 17:01:27 -05:00
|
|
|
scope.isolate(),
|
2019-12-20 14:54:20 -05:00
|
|
|
&source,
|
|
|
|
options,
|
|
|
|
no_cache_reason,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|