summaryrefslogtreecommitdiff
path: root/core/isolate.rs
diff options
context:
space:
mode:
authorandy finch <andyfinch7@gmail.com>2019-03-18 20:03:37 -0400
committerRyan Dahl <ry@tinyclouds.org>2019-03-18 20:03:37 -0400
commitcdfd32dd74d6286afe99fb5400e3dc0e9f2cec49 (patch)
treee8daa342c70ee472de28158d5448ed2078d0d9e9 /core/isolate.rs
parent34a2aa4de6cfe334cf0d26620d244d5c66a976a4 (diff)
Re-implement init scripts in core (#1958)
Re-enables arm64 CI test
Diffstat (limited to 'core/isolate.rs')
-rw-r--r--core/isolate.rs45
1 files changed, 38 insertions, 7 deletions
diff --git a/core/isolate.rs b/core/isolate.rs
index 99e88f553..2833a4c3d 100644
--- a/core/isolate.rs
+++ b/core/isolate.rs
@@ -42,11 +42,26 @@ impl Future for PendingOp {
}
}
+/// Stores a script used to initalize a Isolate
+pub struct StartupScript {
+ pub source: String,
+ pub filename: String,
+}
+
+/// Represents data used to initialize isolate at startup
+/// either a binary snapshot or a javascript source file
+/// in the form of the StartupScript struct.
+pub enum StartupData {
+ Script(StartupScript),
+ Snapshot(deno_buf),
+}
+
/// Defines the behavior of an Isolate.
pub trait Behavior {
- /// Called exactly once when an Isolate is created to retrieve the startup
- /// snapshot.
- fn startup_snapshot(&mut self) -> Option<deno_buf>;
+ /// Allow for a behavior to define the snapshot or script used at
+ /// startup to initalize the isolate. Called exactly once when an
+ /// Isolate is created.
+ fn startup_data(&mut self) -> Option<StartupData>;
/// Called during mod_instantiate() to resolve imports.
fn resolve(&mut self, specifier: &str, referrer: deno_mod) -> deno_mod;
@@ -96,9 +111,15 @@ impl<B: Behavior> Isolate<B> {
let shared = SharedQueue::new(RECOMMENDED_SIZE);
let needs_init = true;
+ // Seperate into Option values for eatch startup type
+ let (startup_snapshot, startup_script) = match behavior.startup_data() {
+ Some(StartupData::Snapshot(d)) => (Some(d), None),
+ Some(StartupData::Script(d)) => (None, Some(d)),
+ None => (None, None),
+ };
let config = libdeno::deno_config {
will_snapshot: 0,
- load_snapshot: match behavior.startup_snapshot() {
+ load_snapshot: match startup_snapshot {
Some(s) => s,
None => libdeno::deno_buf::empty(),
},
@@ -107,14 +128,24 @@ impl<B: Behavior> Isolate<B> {
};
let libdeno_isolate = unsafe { libdeno::deno_new(config) };
- Self {
+ let mut core_isolate = Self {
libdeno_isolate,
behavior,
shared,
needs_init,
pending_ops: Vec::new(),
polled_recently: false,
- }
+ };
+
+ // If we want to use execute this has to happen here sadly.
+ match startup_script {
+ Some(s) => core_isolate
+ .execute(s.filename.as_str(), s.source.as_str())
+ .unwrap(),
+ None => {}
+ };
+
+ core_isolate
}
/// Executes a bit of built-in JavaScript to provide Deno._sharedQueue.
@@ -475,7 +506,7 @@ mod tests {
}
impl Behavior for TestBehavior {
- fn startup_snapshot(&mut self) -> Option<deno_buf> {
+ fn startup_data(&mut self) -> Option<StartupData> {
None
}