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
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use isolate::Buf;
use isolate::IsolateState;
use msg;
use resources;
use resources::Resource;
use resources::ResourceId;
use workers;
use futures::Future;
use serde_json;
use std::sync::Arc;
use std::sync::Mutex;
lazy_static! {
static ref c_rid: Mutex<Option<ResourceId>> = Mutex::new(None);
}
// This corresponds to JS ModuleMetaData.
// TODO Rename one or the other so they correspond.
#[derive(Debug)]
pub struct CodeFetchOutput {
pub module_name: String,
pub filename: String,
pub media_type: msg::MediaType,
pub source_code: String,
pub maybe_output_code: Option<String>,
pub maybe_source_map: Option<String>,
}
impl CodeFetchOutput {
pub fn js_source<'a>(&'a self) -> String {
if self.media_type == msg::MediaType::Json {
return String::from(format!("export default {};", self.source_code));
}
match self.maybe_output_code {
None => self.source_code.clone(),
Some(ref output_code) => output_code.clone(),
}
}
}
impl CodeFetchOutput {
// TODO Use serde_derive? Use flatbuffers?
fn from_json(json_str: &str) -> Option<Self> {
match serde_json::from_str::<serde_json::Value>(json_str) {
Ok(serde_json::Value::Object(map)) => {
let module_name = match map["moduleId"].as_str() {
None => return None,
Some(s) => s.to_string(),
};
let filename = match map["fileName"].as_str() {
None => return None,
Some(s) => s.to_string(),
};
let source_code = match map["sourceCode"].as_str() {
None => return None,
Some(s) => s.to_string(),
};
let maybe_output_code =
map["outputCode"].as_str().map(|s| s.to_string());
let maybe_source_map = map["sourceMap"].as_str().map(|s| s.to_string());
Some(CodeFetchOutput {
module_name,
filename,
media_type: msg::MediaType::JavaScript, // TODO
source_code,
maybe_output_code,
maybe_source_map,
})
}
_ => None,
}
}
}
fn lazy_start(parent_state: &Arc<IsolateState>) -> Resource {
let mut cell = c_rid.lock().unwrap();
let rid = cell.get_or_insert_with(|| {
let resource =
workers::spawn(parent_state.clone(), "compilerMain()".to_string());
resource.rid
});
Resource { rid: *rid }
}
fn req(specifier: &str, referrer: &str) -> Buf {
json!({
"specifier": specifier,
"referrer": referrer,
}).to_string()
.into_boxed_str()
.into_boxed_bytes()
}
pub fn compile_sync(
parent_state: &Arc<IsolateState>,
specifier: &str,
referrer: &str,
) -> Option<CodeFetchOutput> {
let req_msg = req(specifier, referrer);
let compiler = lazy_start(parent_state);
let send_future = resources::worker_post_message(compiler.rid, req_msg);
send_future.wait().unwrap();
let recv_future = resources::worker_recv_message(compiler.rid);
let res_msg = recv_future.wait().unwrap().unwrap();
let res_json = std::str::from_utf8(&res_msg).unwrap();
CodeFetchOutput::from_json(res_json)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compile_sync() {
let cwd = std::env::current_dir().unwrap();
let cwd_string = cwd.to_str().unwrap().to_owned();
let specifier = "./tests/002_hello.ts";
let referrer = cwd_string + "/";
let cfo =
compile_sync(&IsolateState::mock(), specifier, &referrer).unwrap();
let output_code = cfo.maybe_output_code.unwrap();
assert!(output_code.starts_with("console.log(\"Hello World\");"));
}
#[test]
fn code_fetch_output_from_json() {
let json = r#"{
"moduleId":"/Users/rld/src/deno/tests/002_hello.ts",
"fileName":"/Users/rld/src/deno/tests/002_hello.ts",
"mediaType":1,
"sourceCode":"console.log(\"Hello World\");\n",
"outputCode":"yyy",
"sourceMap":"xxx",
"scriptVersion":"1"
}"#;
let actual = CodeFetchOutput::from_json(json).unwrap();
assert_eq!(actual.filename, "/Users/rld/src/deno/tests/002_hello.ts");
assert_eq!(actual.module_name, "/Users/rld/src/deno/tests/002_hello.ts");
assert_eq!(actual.source_code, "console.log(\"Hello World\");\n");
assert_eq!(actual.maybe_output_code, Some("yyy".to_string()));
assert_eq!(actual.maybe_source_map, Some("xxx".to_string()));
}
}
|