1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
extern crate deno;
extern crate serde;
extern crate serde_json;

mod ops;
use deno::deno_mod;
use deno::js_check;
pub use deno::v8_set_flags;
use deno::ErrBox;
use deno::Isolate;
use deno::ModuleSpecifier;
use deno::StartupData;
pub use ops::EmitResult;
use ops::WrittenFile;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;

/// Sets the --trace-serializer V8 flag for debugging snapshots.
pub fn trace_serializer() {
  let dummy = "foo".to_string();
  let r =
    deno::v8_set_flags(vec![dummy.clone(), "--trace-serializer".to_string()]);
  assert_eq!(r, vec![dummy]);
}

#[derive(Debug)]
pub struct TSState {
  bundle: bool,
  exit_code: i32,

  emit_result: Option<EmitResult>,
  // (url, corresponding_module, source_code)
  written_files: Vec<WrittenFile>,
}

pub struct TSIsolate {
  isolate: Isolate,
  state: Arc<Mutex<TSState>>,
}

static TYPESCRIPT_CODE: &str = include_str!("assets/typescript.js");

impl TSIsolate {
  fn new(bundle: bool) -> TSIsolate {
    let mut isolate = Isolate::new(StartupData::None, false);
    let main_code = include_str!("compiler_main.js");
    js_check(isolate.execute("assets/typescript.js", TYPESCRIPT_CODE));
    js_check(isolate.execute("compiler_main.js", main_code));

    let state = Arc::new(Mutex::new(TSState {
      bundle,
      exit_code: 0,
      emit_result: None,
      written_files: Vec::new(),
    }));
    let state_ = state.clone();
    isolate.set_dispatch(move |op_id, control_buf, zero_copy_buf| {
      assert!(zero_copy_buf.is_none()); // zero_copy_buf unused in compiler.
      let mut s = state_.lock().unwrap();
      ops::dispatch_op(&mut s, op_id, control_buf)
    });
    TSIsolate { isolate, state }
  }

  // TODO(ry) Instead of Result<Arc<Mutex<TSState>>, ErrBox>, return something
  // like Result<TSState, ErrBox>. I think it would be nicer if this function
  // consumes TSIsolate.
  /// Compiles each module to ESM. Doesn't write any files to disk.
  /// Passes all output via state.
  fn compile(
    mut self,
    config_json: &serde_json::Value,
    root_names: Vec<String>,
  ) -> Result<Arc<Mutex<TSState>>, ErrBox> {
    let root_names_json = serde_json::json!(root_names).to_string();
    let source =
      &format!("main({:?}, {})", config_json.to_string(), root_names_json);
    self.isolate.execute("<anon>", source)?;
    Ok(self.state.clone())
  }
}

// TODO(ry) Instead of Result<Arc<Mutex<TSState>>, ErrBox>, return something
// like Result<TSState, ErrBox>
pub fn compile(
  root_names: Vec<PathBuf>,
) -> Result<Arc<Mutex<TSState>>, ErrBox> {
  let ts_isolate = TSIsolate::new(false);

  let config_json = serde_json::json!({
    "compilerOptions": {
      "declaration": true,
      "lib": ["esnext"],
      "module": "esnext",
      "target": "esnext",
      "listFiles": true,
      "listEmittedFiles": true,
      // Emit the source alongside the sourcemaps within a single file;
      // requires --inlineSourceMap or --sourceMap to be set.
      // "inlineSources": true,
      "sourceMap": true,
    },
  });

  let mut root_names_str: Vec<String> = root_names
    .iter()
    .map(|p| {
      assert!(p.exists());
      p.to_string_lossy().to_string()
    })
    .collect();
  root_names_str.push("$asset$/lib.deno_core.d.ts".to_string());

  // TODO lift js_check to caller?
  let state = js_check(ts_isolate.compile(&config_json, root_names_str));

  Ok(state)
}

pub fn compile_bundle(
  bundle: &Path,
  root_names: Vec<PathBuf>,
) -> Result<Arc<Mutex<TSState>>, ErrBox> {
  let ts_isolate = TSIsolate::new(true);

  let config_json = serde_json::json!({
    "compilerOptions": {
      "declaration": true,
      "lib": ["esnext"],
      "module": "amd",
      "target": "esnext",
      "listFiles": true,
      "listEmittedFiles": true,
      // "types" : ["typescript.d.ts"],
      "typeRoots" : ["$typeRoots$"],
      // Emit the source alongside the sourcemaps within a single file;
      // requires --inlineSourceMap or --sourceMap to be set.
      // "inlineSources": true,
      "sourceMap": true,
      "outFile": bundle,
    },
  });

  let mut root_names_str: Vec<String> = root_names
    .iter()
    .map(|p| {
      assert!(p.exists());

      let module_specifier =
        ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
      module_specifier.as_str().to_string()
      // p.to_string_lossy().to_string()
    })
    .collect();
  root_names_str.push("$asset$/lib.deno_core.d.ts".to_string());

  // TODO lift js_check to caller?
  let state = js_check(ts_isolate.compile(&config_json, root_names_str));

  Ok(state)
}

#[allow(dead_code)]
fn print_source_code(code: &str) {
  let mut i = 1;
  for line in code.lines() {
    println!("{:3}  {}", i, line);
    i += 1;
  }
}

pub fn mksnapshot_bundle(
  bundle: &Path,
  env_var: &str,
  state: Arc<Mutex<TSState>>,
) -> Result<(), ErrBox> {
  let mut runtime_isolate = Isolate::new(StartupData::None, true);
  let source_code_vec = std::fs::read(bundle)?;
  let source_code = std::str::from_utf8(&source_code_vec)?;

  js_check(runtime_isolate.execute("almond_.js", include_str!("almond.js")));

  js_check(runtime_isolate.execute("bundle", &source_code));

  // execute main.

  let state = state.lock().unwrap();
  let main = state.written_files.last().unwrap().module_name.clone();
  // let main = "file:///Users/rld/src/deno_typescript/cli_snapshots/js/main.ts";
  js_check(runtime_isolate.execute("anon", &format!("require('{}')", main)));

  snapshot_to_env(runtime_isolate, env_var)?;

  Ok(())
}

pub fn mksnapshot_bundle_ts(
  bundle: &Path,
  env_var: &str,
  state: Arc<Mutex<TSState>>,
) -> Result<(), ErrBox> {
  let mut runtime_isolate = Isolate::new(StartupData::None, true);
  let source_code_vec = std::fs::read(bundle)?;
  let source_code = std::str::from_utf8(&source_code_vec)?;

  js_check(runtime_isolate.execute("almond_.js", include_str!("almond.js")));
  js_check(runtime_isolate.execute(&bundle.to_string_lossy(), &source_code));

  let state = state.lock().unwrap();
  let main = state.written_files.last().unwrap().module_name.clone();

  js_check(runtime_isolate.execute("typescript.js", TYPESCRIPT_CODE));
  js_check(runtime_isolate.execute("anon", &format!("require('{}')", main)));

  snapshot_to_env(runtime_isolate, env_var)?;

  Ok(())
}

// TODO(ry) Instead of state: Arc<Mutex<TSState>>, take something like state:
// &TSState
pub fn mksnapshot(
  env_var: &str,
  state: Arc<Mutex<TSState>>,
) -> Result<(), ErrBox> {
  assert!(!state.lock().unwrap().bundle);

  let mut runtime_isolate = Isolate::new(StartupData::None, true);
  let mut url2id: HashMap<String, deno_mod> = HashMap::new();
  let mut id2url: HashMap<deno_mod, String> = HashMap::new();

  let state = state.lock().unwrap();

  let main = state.written_files.last().unwrap().module_name.clone();

  for f in state.written_files.iter() {
    if f.url.ends_with(".js") {
      let is_main = f.module_name == main;
      let id =
        js_check(runtime_isolate.mod_new(is_main, &f.url, &f.source_code));
      url2id.insert(f.module_name.clone(), id);
      id2url.insert(id, f.module_name.clone());

      if f.url.ends_with("flatbuffers/flatbuffers.js") {
        print_source_code(&f.source_code);
      }
    }
  }

  let url2id_ = url2id.clone(); // FIXME
  let mut resolve = move |specifier: &str, referrer: deno_mod| -> deno_mod {
    let referrer_url = id2url.get(&referrer).unwrap();
    let import_url =
      ModuleSpecifier::resolve_import(specifier, referrer_url.as_str())
        .unwrap();
    let import_url_str = import_url.as_str();
    *url2id_.get(import_url_str).unwrap_or_else(|| {
      panic!("Could not resolve {}", import_url_str);
    })
  };

  // Instantiate each module.
  for (_url, id) in url2id.iter() {
    js_check(runtime_isolate.mod_instantiate(*id, &mut resolve));
  }

  // Execute the main module.
  let main_id = url2id.get(main.as_str()).unwrap();
  js_check(runtime_isolate.mod_evaluate(*main_id));

  snapshot_to_env(runtime_isolate, env_var)?;
  Ok(())
}

fn snapshot_to_env(
  runtime_isolate: Isolate,
  env_var: &str,
) -> Result<(), ErrBox> {
  println!("creating snapshot...");
  let snapshot = runtime_isolate.snapshot()?;
  let snapshot_slice =
    unsafe { std::slice::from_raw_parts(snapshot.data_ptr, snapshot.data_len) };
  println!("snapshot bytes {}", snapshot_slice.len());
  //
  let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
  let snapshot_path = out_dir.join(env_var);

  fs::write(&snapshot_path, snapshot_slice)?;
  println!("snapshot path {} ", snapshot_path.display());
  println!("cargo:rustc-env={}={}", env_var, snapshot_path.display());
  Ok(())
}

pub fn get_asset(asset: &str) -> String {
  match asset {
    "lib.deno_core.d.ts" => {
      include_str!("assets/lib.deno_core.d.ts").to_string()
    }
    "lib.esnext.d.ts" => include_str!("assets/lib.esnext.d.ts").to_string(),
    "lib.es2019.d.ts" => include_str!("assets/lib.es2019.d.ts").to_string(),
    "lib.es2018.d.ts" => include_str!("assets/lib.es2018.d.ts").to_string(),
    "lib.es2017.d.ts" => include_str!("assets/lib.es2017.d.ts").to_string(),
    "lib.es2016.d.ts" => include_str!("assets/lib.es2016.d.ts").to_string(),
    "lib.es5.d.ts" => include_str!("assets/lib.es5.d.ts").to_string(),
    "lib.es2015.d.ts" => include_str!("assets/lib.es2015.d.ts").to_string(),
    "lib.es2015.core.d.ts" => {
      include_str!("assets/lib.es2015.core.d.ts").to_string()
    }
    "lib.es2015.collection.d.ts" => {
      include_str!("assets/lib.es2015.collection.d.ts").to_string()
    }
    "lib.es2015.generator.d.ts" => {
      include_str!("assets/lib.es2015.generator.d.ts").to_string()
    }
    "lib.es2015.iterable.d.ts" => {
      include_str!("assets/lib.es2015.iterable.d.ts").to_string()
    }
    "lib.es2015.promise.d.ts" => {
      include_str!("assets/lib.es2015.promise.d.ts").to_string()
    }
    "lib.es2015.symbol.d.ts" => {
      include_str!("assets/lib.es2015.symbol.d.ts").to_string()
    }
    "lib.es2015.proxy.d.ts" => {
      include_str!("assets/lib.es2015.proxy.d.ts").to_string()
    }
    "lib.es2015.symbol.wellknown.d.ts" => {
      include_str!("assets/lib.es2015.symbol.wellknown.d.ts").to_string()
    }
    "lib.es2015.reflect.d.ts" => {
      include_str!("assets/lib.es2015.reflect.d.ts").to_string()
    }
    "lib.es2016.array.include.d.ts" => {
      include_str!("assets/lib.es2016.array.include.d.ts").to_string()
    }
    "lib.es2017.object.d.ts" => {
      include_str!("assets/lib.es2017.object.d.ts").to_string()
    }
    "lib.es2017.sharedmemory.d.ts" => {
      include_str!("assets/lib.es2017.sharedmemory.d.ts").to_string()
    }
    "lib.es2017.string.d.ts" => {
      include_str!("assets/lib.es2017.string.d.ts").to_string()
    }
    "lib.es2017.intl.d.ts" => {
      include_str!("assets/lib.es2017.intl.d.ts").to_string()
    }
    "lib.es2017.typedarrays.d.ts" => {
      include_str!("assets/lib.es2017.typedarrays.d.ts").to_string()
    }
    "lib.es2018.asynciterable.d.ts" => {
      include_str!("assets/lib.es2018.asynciterable.d.ts").to_string()
    }
    "lib.es2018.promise.d.ts" => {
      include_str!("assets/lib.es2018.promise.d.ts").to_string()
    }
    "lib.es2018.regexp.d.ts" => {
      include_str!("assets/lib.es2018.regexp.d.ts").to_string()
    }
    "lib.es2018.intl.d.ts" => {
      include_str!("assets/lib.es2018.intl.d.ts").to_string()
    }
    "lib.es2019.array.d.ts" => {
      include_str!("assets/lib.es2019.array.d.ts").to_string()
    }
    "lib.es2019.object.d.ts" => {
      include_str!("assets/lib.es2019.object.d.ts").to_string()
    }
    "lib.es2019.string.d.ts" => {
      include_str!("assets/lib.es2019.string.d.ts").to_string()
    }
    "lib.es2019.symbol.d.ts" => {
      include_str!("assets/lib.es2019.symbol.d.ts").to_string()
    }
    "lib.esnext.bigint.d.ts" => {
      include_str!("assets/lib.esnext.bigint.d.ts").to_string()
    }
    "lib.esnext.intl.d.ts" => {
      include_str!("assets/lib.esnext.intl.d.ts").to_string()
    }
    _ => panic!("Unknown asset {}", asset),
  }
}