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
use crate::error::type_error;
use crate::error::AnyError;
use crate::include_js_files;
use crate::op_sync;
use crate::resources::ResourceId;
use crate::Extension;
use crate::OpState;
use std::io::{stderr, stdout, Write};
pub(crate) fn init_builtins() -> Extension {
Extension::builder()
.js(include_js_files!(
prefix "deno:core",
"00_primordials.js",
"01_core.js",
"02_error.js",
))
.ops(vec![
("op_close", op_sync(op_close)),
("op_print", op_sync(op_print)),
("op_resources", op_sync(op_resources)),
])
.build()
}
pub fn op_resources(
state: &mut OpState,
_args: (),
_: (),
) -> Result<Vec<(ResourceId, String)>, AnyError> {
let serialized_resources = state
.resource_table
.names()
.map(|(rid, name)| (rid, name.to_string()))
.collect();
Ok(serialized_resources)
}
pub fn op_close(
state: &mut OpState,
rid: Option<ResourceId>,
_: (),
) -> Result<(), AnyError> {
let rid = rid.ok_or_else(|| type_error("missing or invalid `rid`"))?;
state.resource_table.close(rid)?;
Ok(())
}
pub fn op_print(
_state: &mut OpState,
msg: String,
is_err: bool,
) -> Result<(), AnyError> {
if is_err {
stderr().write_all(msg.as_bytes())?;
stderr().flush().unwrap();
} else {
stdout().write_all(msg.as_bytes())?;
stdout().flush().unwrap();
}
Ok(())
}