Skip to content

feat(jsruntime): async/await #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
babd66f
feat(jstb): add `--as`
masnagam Aug 12, 2024
19d0e12
feat(jsparser): `Module` and `AwaitExpression`
masnagam Aug 12, 2024
39093b1
feat(jsruntime)!: dump only when "1" is set
masnagam Sep 28, 2024
6652225
style(jsruntime): format
masnagam Sep 28, 2024
89f5c53
feat(jsruntime): implement async/await
masnagam Oct 14, 2024
fd00f92
test(jsruntime): add test cases
masnagam Oct 14, 2024
5b2d0ee
feat(jsruntime): await promise
masnagam Oct 14, 2024
422733f
feat(jsruntime): top-level await expressions in modules
masnagam Oct 15, 2024
5f47da8
feat(jsruntime): AsyncFunctionExpression
masnagam Oct 15, 2024
5e9e918
feat(jsruntime): AsyncArrowFunction
masnagam Oct 18, 2024
971d65d
refactor(jsruntime): refactoring
masnagam Oct 19, 2024
e511aac
style(jsruntime): format
masnagam Oct 20, 2024
7ac2f09
test(jsruntime): reorganize files
masnagam Oct 20, 2024
33a4689
fix(jsparser): StatementList_Await
masnagam Oct 20, 2024
a17298f
test(jsruntime): add test cases
masnagam Oct 20, 2024
e311dab
feat(jsruntime): simplify the scope cleanup checker
masnagam Oct 21, 2024
d538f55
refactor(jsruntime): refactoring
masnagam Oct 21, 2024
562beab
refactor(jsruntime): refactoring
masnagam Oct 22, 2024
6885847
fix(jsruntime): support the scope cleanup checker in coroutines
masnagam Oct 24, 2024
973d661
refactor(jsruntime): pass FunctionId to peer
masnagam Oct 25, 2024
e42e274
refactor(jsruntime): refactoring
masnagam Oct 25, 2024
d5ba26c
feat(jsruntime): save operands to the scratch buffer
masnagam Oct 26, 2024
0272209
fix(jsruntime): await_in_if_else.mjs
masnagam Oct 27, 2024
c9e476d
refactor(jsruntime): refactoring for the switch statement
masnagam Oct 27, 2024
74699f4
fix(jsruntime): await_in_switch_case.mjs
masnagam Oct 28, 2024
f8ba021
style(jsruntime): format
masnagam Oct 28, 2024
0716a89
feat(jsruntime): compute the size of the scratch buffer
masnagam Oct 28, 2024
a6845f7
fix(jsruntime): await_in_arguments.mjs
masnagam Oct 28, 2024
16a9255
fix(jsruntime): save closures and promises to the scratch buffer
masnagam Oct 29, 2024
74214b3
fix(jsruntime): await_in_arguments.mjs
masnagam Oct 29, 2024
92e362e
build(deps): update Cargo.lock
masnagam Oct 29, 2024
09b9159
fix(jstb): remove unused crates
masnagam Oct 29, 2024
14c66e1
fix(jsparser): remove unused hidden symbols
masnagam Oct 29, 2024
e85b53a
refactor(jsruntime): refactoring
masnagam Oct 29, 2024
37024a6
fix(jsruntime): fix a borrow checker issue
masnagam Oct 29, 2024
92a908f
refactor(jsruntime): refactoring
masnagam Oct 30, 2024
3839188
fix(jsruntime): fix comments
masnagam Oct 30, 2024
c1863b3
docs(jsruntime): implementation notes
masnagam Oct 30, 2024
66f5337
refactor(jsruntime): refactoring
masnagam Oct 31, 2024
00ebc6f
refactor(jsruntime): refactoring
masnagam Oct 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
355 changes: 159 additions & 196 deletions Cargo.lock

Large diffs are not rendered by default.

137 changes: 79 additions & 58 deletions bins/estree/src/builder/actions.yaml

Large diffs are not rendered by default.

53 changes: 48 additions & 5 deletions bins/jstb/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ struct CommandLine {
#[command(subcommand)]
command: Command,

/// The type of the source text.
///
/// Specify `module` explicitly when the source text read from STDIN is parsed as a module.
#[arg(
global = true,
long = "as",
default_value = "auto",
value_name = "SOURCE_TYPE"
)]
parse_as: SourceType,

/// Enables the scope cleanup checker.
#[arg(global = true, long)]
scope_cleanup_checker: bool,
Expand Down Expand Up @@ -65,6 +76,20 @@ struct Run {
no_optimize: bool,
}

#[derive(clap::ValueEnum, Clone)]
enum SourceType {
/// Parse as a script if the file extension of the input file is "js".
/// Parse as a module if the file extension of the input file is "mjs".
/// Otherwise, parse as a script.
Auto,

/// Parse as a script.
Script,

/// Parse as a module.
Module,
}

fn main() -> Result<()> {
logging::init();

Expand All @@ -80,9 +105,26 @@ fn main() -> Result<()> {

let source = read_source(cl.source.as_ref())?;

// This is not a good practice, but we define a macro instead of a function in order to avoid
// code clones. By using the macro, we can avoid additional `use` directives needed for the
// return type.
macro_rules! parse {
($source:expr, $cl:expr) => {
match $cl.parse_as {
SourceType::Auto => match $cl.source.as_ref().and_then(|path| path.extension()) {
Some(ext) if ext == "js" => runtime.parse_script($source),
Some(ext) if ext == "mjs" => runtime.parse_module($source),
_ => runtime.parse_script($source),
},
SourceType::Script => runtime.parse_script($source),
SourceType::Module => runtime.parse_module($source),
}
};
}

match cl.command {
Command::Parse(args) => {
let program = runtime.parse_script(&source)?;
let program = parse!(&source, cl)?;
for kind in args.print.chars() {
match kind {
'f' => {
Expand All @@ -99,15 +141,16 @@ fn main() -> Result<()> {
}
Command::Compile(args) => {
runtime.enable_llvmir_labels();
let program = runtime.parse_script(&source)?;
let program = parse!(&source, cl)?;
let module = runtime.compile(&program, !args.no_optimize)?;
module.print(false); // to STDOUT
}
Command::Run(args) => {
let program = runtime.parse_script(&source)?;
let program = parse!(&source, cl)?;
let module = runtime.compile(&program, !args.no_optimize)?;
if let Err(v) = runtime.evaluate(module) {
println!("Uncaught {v:?}");
match runtime.evaluate(module) {
Ok(_) => runtime.run(),
Err(v) => println!("Uncaught {v:?}"),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions libs/jsparser/src/parser/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ GOAL_SYMBOLS := \
ArrowFormalParameters \
ArrowFormalParameters_Yield \
ArrowFormalParameters_Await \
ArrowFormalParameters_Yield_Await

ArrowFormalParameters_Yield_Await \
AsyncArrowHead

# targets

Expand Down
15 changes: 14 additions & 1 deletion libs/jsparser/src/symbol/builtins.rs.njk
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@ use super::SymbolRegistry;

impl Symbol {
{%- for symbol in data %}
pub const {{ symbol | upper }}: Self = Self({{ loop.index0 + 1 }});
{%- if symbol.startsWith('##') %}
pub const HIDDEN_{{ symbol | constantCase }}: Self = Self({{ loop.index }});
{%- else %}
{#- Use `upper` instead of `constantCase` so that 'NaN' is converted into 'NAN'. #}
pub const {{ symbol | upper }}: Self = Self({{ loop.index }});
{%- endif %}
{%- endfor %}

pub fn is_hidden(&self) -> bool {
self.0 <= {{ data | select('startsWith', '##') | length }}
}
}

impl SymbolRegistry {
pub(super) fn register_builtin_symbols(&mut self) {
{%- for symbol in data %}
{%- if symbol.startsWith('##') %}
self.register_builtin_symbol(Symbol::HIDDEN_{{ symbol | constantCase }}, "{{ symbol }}");
{%- else %}
self.register_builtin_symbol(Symbol::{{ symbol | upper }}, "{{ symbol }}");
{%- endif %}
{%- endfor %}
}

Expand Down
8 changes: 8 additions & 0 deletions libs/jsparser/src/symbol/builtins.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Hidden symbols must be defined before others.
# Hidden symbols must start with `##`.
- '##coroutine'
- '##promise'
- '##result'
- '##error'

# Reserved words defined in the ECMA-262 specification.
- Infinity
- NaN
- async
Expand Down
1 change: 0 additions & 1 deletion libs/jsparser/src/symbol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub struct Symbol(u32);
impl Symbol {
pub const NONE: Symbol = Symbol(0);

#[inline]
pub fn id(&self) -> u32 {
self.0
}
Expand Down
Loading
Loading