2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-20 19:49:57 -04:00
|
|
|
// Note: this module is used both in build.rs and main.rs.
|
2019-09-02 17:07:11 -04:00
|
|
|
|
2020-01-05 11:56:18 -05:00
|
|
|
pub use deno_core::v8_set_flags;
|
2020-09-05 20:34:02 -04:00
|
|
|
use deno_core::BufVec;
|
2020-04-18 20:05:13 -04:00
|
|
|
use deno_core::Op;
|
2020-09-24 09:29:18 -04:00
|
|
|
use deno_core::OpState;
|
|
|
|
use std::cell::RefCell;
|
2020-02-01 06:02:23 -05:00
|
|
|
use std::collections::HashMap;
|
2019-09-02 17:07:11 -04:00
|
|
|
use std::path::PathBuf;
|
2020-09-05 20:34:02 -04:00
|
|
|
use std::rc::Rc;
|
2019-09-02 17:07:11 -04:00
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
fn get_asset(name: &str) -> Option<&'static str> {
|
2020-01-21 10:24:02 -05:00
|
|
|
macro_rules! inc {
|
|
|
|
($e:expr) => {
|
2020-07-22 12:03:46 -04:00
|
|
|
Some(include_str!(concat!("dts/", $e)))
|
2020-01-21 10:24:02 -05:00
|
|
|
};
|
|
|
|
}
|
2019-09-02 17:07:11 -04:00
|
|
|
match name {
|
2020-02-12 16:41:51 -05:00
|
|
|
"system_loader.js" => Some(include_str!("system_loader.js")),
|
2020-06-18 09:06:48 -04:00
|
|
|
"system_loader_es5.js" => Some(include_str!("system_loader_es5.js")),
|
2020-01-22 14:18:01 -05:00
|
|
|
"bootstrap.ts" => Some("console.log(\"hello deno\");"),
|
2019-09-15 18:36:27 -04:00
|
|
|
"typescript.d.ts" => inc!("typescript.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.dom.d.ts" => inc!("lib.dom.d.ts"),
|
2020-03-04 06:14:43 -05:00
|
|
|
"lib.dom.iterable.d.ts" => inc!("lib.dom.iterable.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.es5.d.ts" => inc!("lib.es5.d.ts"),
|
|
|
|
"lib.es6.d.ts" => inc!("lib.es6.d.ts"),
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.esnext.d.ts" => inc!("lib.esnext.d.ts"),
|
2019-11-13 13:42:34 -05:00
|
|
|
"lib.es2020.d.ts" => inc!("lib.es2020.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.es2020.full.d.ts" => inc!("lib.es2020.full.d.ts"),
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.es2019.d.ts" => inc!("lib.es2019.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.es2019.full.d.ts" => inc!("lib.es2019.full.d.ts"),
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.es2018.d.ts" => inc!("lib.es2018.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.es2018.full.d.ts" => inc!("lib.es2018.full.d.ts"),
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.es2017.d.ts" => inc!("lib.es2017.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.es2017.full.d.ts" => inc!("lib.es2017.full.d.ts"),
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.es2016.d.ts" => inc!("lib.es2016.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.es2016.full.d.ts" => inc!("lib.es2016.full.d.ts"),
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.es2015.d.ts" => inc!("lib.es2015.d.ts"),
|
|
|
|
"lib.es2015.collection.d.ts" => inc!("lib.es2015.collection.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.es2015.core.d.ts" => inc!("lib.es2015.core.d.ts"),
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.es2015.generator.d.ts" => inc!("lib.es2015.generator.d.ts"),
|
|
|
|
"lib.es2015.iterable.d.ts" => inc!("lib.es2015.iterable.d.ts"),
|
|
|
|
"lib.es2015.promise.d.ts" => inc!("lib.es2015.promise.d.ts"),
|
|
|
|
"lib.es2015.proxy.d.ts" => inc!("lib.es2015.proxy.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.es2015.reflect.d.ts" => inc!("lib.es2015.reflect.d.ts"),
|
|
|
|
"lib.es2015.symbol.d.ts" => inc!("lib.es2015.symbol.d.ts"),
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.es2015.symbol.wellknown.d.ts" => {
|
|
|
|
inc!("lib.es2015.symbol.wellknown.d.ts")
|
|
|
|
}
|
|
|
|
"lib.es2016.array.include.d.ts" => inc!("lib.es2016.array.include.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.es2017.intl.d.ts" => inc!("lib.es2017.intl.d.ts"),
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.es2017.object.d.ts" => inc!("lib.es2017.object.d.ts"),
|
|
|
|
"lib.es2017.sharedmemory.d.ts" => inc!("lib.es2017.sharedmemory.d.ts"),
|
|
|
|
"lib.es2017.string.d.ts" => inc!("lib.es2017.string.d.ts"),
|
|
|
|
"lib.es2017.typedarrays.d.ts" => inc!("lib.es2017.typedarrays.d.ts"),
|
2019-09-17 12:24:44 -04:00
|
|
|
"lib.es2018.asyncgenerator.d.ts" => inc!("lib.es2018.asyncgenerator.d.ts"),
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.es2018.asynciterable.d.ts" => inc!("lib.es2018.asynciterable.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.es2018.intl.d.ts" => inc!("lib.es2018.intl.d.ts"),
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.es2018.promise.d.ts" => inc!("lib.es2018.promise.d.ts"),
|
|
|
|
"lib.es2018.regexp.d.ts" => inc!("lib.es2018.regexp.d.ts"),
|
|
|
|
"lib.es2019.array.d.ts" => inc!("lib.es2019.array.d.ts"),
|
|
|
|
"lib.es2019.object.d.ts" => inc!("lib.es2019.object.d.ts"),
|
|
|
|
"lib.es2019.string.d.ts" => inc!("lib.es2019.string.d.ts"),
|
|
|
|
"lib.es2019.symbol.d.ts" => inc!("lib.es2019.symbol.d.ts"),
|
2020-02-25 15:33:19 -05:00
|
|
|
"lib.es2020.bigint.d.ts" => inc!("lib.es2020.bigint.d.ts"),
|
2020-08-24 19:43:54 -04:00
|
|
|
"lib.es2020.intl.d.ts" => inc!("lib.es2020.intl.d.ts"),
|
2020-02-25 15:33:19 -05:00
|
|
|
"lib.es2020.promise.d.ts" => inc!("lib.es2020.promise.d.ts"),
|
2019-11-13 13:42:34 -05:00
|
|
|
"lib.es2020.string.d.ts" => inc!("lib.es2020.string.d.ts"),
|
|
|
|
"lib.es2020.symbol.wellknown.d.ts" => {
|
|
|
|
inc!("lib.es2020.symbol.wellknown.d.ts")
|
|
|
|
}
|
2019-09-02 17:07:11 -04:00
|
|
|
"lib.esnext.intl.d.ts" => inc!("lib.esnext.intl.d.ts"),
|
2020-05-13 08:03:04 -04:00
|
|
|
"lib.esnext.promise.d.ts" => inc!("lib.esnext.promise.d.ts"),
|
|
|
|
"lib.esnext.string.d.ts" => inc!("lib.esnext.string.d.ts"),
|
2020-02-19 00:34:11 -05:00
|
|
|
"lib.scripthost.d.ts" => inc!("lib.scripthost.d.ts"),
|
|
|
|
"lib.webworker.d.ts" => inc!("lib.webworker.d.ts"),
|
|
|
|
"lib.webworker.importscripts.d.ts" => {
|
|
|
|
inc!("lib.webworker.importscripts.d.ts")
|
|
|
|
}
|
2019-09-02 17:07:11 -04:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 09:14:27 -05:00
|
|
|
/// Warning: Returns a non-JSON op dispatcher. Must be manually attached to
|
2020-09-06 15:44:29 -04:00
|
|
|
/// JsRuntime.
|
2020-09-24 09:29:18 -04:00
|
|
|
pub fn op_fetch_asset(
|
|
|
|
custom_assets: HashMap<String, PathBuf>,
|
|
|
|
) -> impl Fn(Rc<RefCell<OpState>>, BufVec) -> Op {
|
2020-04-07 11:12:31 -04:00
|
|
|
for (_, path) in custom_assets.iter() {
|
|
|
|
println!("cargo:rerun-if-changed={}", path.display());
|
|
|
|
}
|
2020-09-24 09:29:18 -04:00
|
|
|
move |_state: Rc<RefCell<OpState>>, bufs: BufVec| -> Op {
|
2020-09-05 20:34:02 -04:00
|
|
|
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
|
|
|
|
let name = std::str::from_utf8(&bufs[0]).unwrap();
|
2020-02-25 09:14:27 -05:00
|
|
|
|
|
|
|
let asset_code = if let Some(source_code) = get_asset(name) {
|
|
|
|
source_code.to_string()
|
|
|
|
} else if let Some(asset_path) = custom_assets.get(name) {
|
|
|
|
let source_code_vec =
|
|
|
|
std::fs::read(&asset_path).expect("Asset not found");
|
|
|
|
let source_code = std::str::from_utf8(&source_code_vec).unwrap();
|
|
|
|
source_code.to_string()
|
|
|
|
} else {
|
|
|
|
panic!("fetch_asset bad asset {}", name)
|
|
|
|
};
|
|
|
|
|
|
|
|
let vec = asset_code.into_bytes();
|
|
|
|
deno_core::Op::Sync(vec.into_boxed_slice())
|
|
|
|
}
|
|
|
|
}
|