js,os: fix `return` in `or` blocks; Properly get path_delimiter and path_separator in JS (#13179)

pull/13180/head
playX 2022-01-15 10:55:03 +03:00 committed by GitHub
parent 9fd65b5b18
commit 7fe62a8b3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 10 deletions

View File

@ -1,17 +1,33 @@
module os module os
$if js_node { $if js_node {
#const $fs = require('fs'); #var $fs = require('fs');
#const $path = require('path'); #var $path = require('path');
#const tty = require('tty') #var tty = require('tty')
} }
pub const ( pub const (
path_delimiter = '/' path_delimiter = get_path_delimiter()
path_separator = '/' path_separator = get_path_separator()
args = []string{} args = []string{}
) )
fn get_path_delimiter() string {
delimiter := ':'
$if js_node {
#delimiter.str = $path.delimiter
}
return delimiter
}
fn get_path_separator() string {
separator := '/'
$if js_node {
#separator.str = $path.sep
}
return separator
}
fn init() { fn init() {
$if js_node { $if js_node {
#$process.argv.forEach(function(val,index) { os__args.arr[index] = new string(val); }) #$process.argv.forEach(function(val,index) { os__args.arr[index] = new string(val); })

View File

@ -692,7 +692,16 @@ fn (mut g JsGen) gen_method_decl(it ast.FnDecl, typ FnGenType) {
} }
} }
} }
g.inc_indent()
g.writeln('try {')
g.inc_indent()
g.stmts(it.stmts) g.stmts(it.stmts)
g.dec_indent()
g.writeln('} catch (e) { ')
g.writeln('\tif (e instanceof ReturnException) { return e.val; } ')
g.writeln('\tthrow e;')
g.writeln('}')
g.dec_indent()
g.writeln('}') g.writeln('}')
if is_main { if is_main {

View File

@ -482,6 +482,7 @@ pub fn (mut g JsGen) init() {
g.definitions.writeln('function BreakException() {}') g.definitions.writeln('function BreakException() {}')
g.definitions.writeln('function ContinueException() {}') g.definitions.writeln('function ContinueException() {}')
g.definitions.writeln('function ReturnException(val) { this.val = val; }')
} }
pub fn (g JsGen) hashes() string { pub fn (g JsGen) hashes() string {
@ -1624,13 +1625,18 @@ fn (mut g JsGen) gen_for_c_stmt(it ast.ForCStmt) {
g.stmt_no_semi(it.inc) g.stmt_no_semi(it.inc)
} }
g.writeln(') {') g.writeln(') {')
g.inc_indent()
g.writeln('try { ') g.writeln('try { ')
g.inc_indent()
g.stmts(it.stmts) g.stmts(it.stmts)
g.dec_indent()
g.writeln('} catch (e) {') g.writeln('} catch (e) {')
g.writeln(' if (e instanceof BreakException) { break; }') g.writeln(' if (e instanceof BreakException) { break; }')
g.writeln(' else if (e instanceof ContinueException) { continue; }') g.writeln(' else if (e instanceof ContinueException) { continue; }')
g.writeln(' else { throw e; } }') g.writeln(' else { throw e; } }')
g.dec_indent()
g.writeln('}') g.writeln('}')
g.inside_loop = false g.inside_loop = false
} }
@ -1648,12 +1654,16 @@ fn (mut g JsGen) gen_for_in_stmt(it ast.ForInStmt) {
g.expr(it.high) g.expr(it.high)
g.writeln('; $i = new int($i + 1)) {') g.writeln('; $i = new int($i + 1)) {')
g.inside_loop = false g.inside_loop = false
g.inc_indent()
g.writeln('try { ') g.writeln('try { ')
g.inc_indent()
g.stmts(it.stmts) g.stmts(it.stmts)
g.dec_indent()
g.writeln('} catch (e) {') g.writeln('} catch (e) {')
g.writeln(' if (e instanceof BreakException) { break; }') g.writeln(' if (e instanceof BreakException) { break; }')
g.writeln(' else if (e instanceof ContinueException) { continue; }') g.writeln(' else if (e instanceof ContinueException) { continue; }')
g.writeln(' else { throw e; } }') g.writeln(' else { throw e; } }')
g.dec_indent()
g.writeln('}') g.writeln('}')
} else if it.kind in [.array, .string] || it.cond_type.has_flag(.variadic) { } else if it.kind in [.array, .string] || it.cond_type.has_flag(.variadic) {
// `for num in nums {` // `for num in nums {`
@ -1698,12 +1708,16 @@ fn (mut g JsGen) gen_for_in_stmt(it ast.ForInStmt) {
} }
} }
g.writeln(') {') g.writeln(') {')
g.inc_indent()
g.writeln('try { ') g.writeln('try { ')
g.inc_indent()
g.stmts(it.stmts) g.stmts(it.stmts)
g.dec_indent()
g.writeln('} catch (e) {') g.writeln('} catch (e) {')
g.writeln(' if (e instanceof BreakException) { break; }') g.writeln(' if (e instanceof BreakException) { break; }')
g.writeln(' else if (e instanceof ContinueException) { continue; }') g.writeln(' else if (e instanceof ContinueException) { continue; }')
g.writeln(' else { throw e; } }') g.writeln(' else { throw e; } }')
g.dec_indent()
g.writeln('}') g.writeln('}')
} else if it.kind == .map { } else if it.kind == .map {
// `for key, val in map[string]int {` // `for key, val in map[string]int {`
@ -1748,8 +1762,11 @@ fn (mut g JsGen) gen_for_in_stmt(it ast.ForInStmt) {
g.inc_indent() g.inc_indent()
g.writeln('let $val = ${tmp}.map[$tmp2];') g.writeln('let $val = ${tmp}.map[$tmp2];')
g.writeln('let $key = $tmp2;') g.writeln('let $key = $tmp2;')
g.writeln('try { ') g.writeln('try { ')
g.inc_indent()
g.stmts(it.stmts) g.stmts(it.stmts)
g.dec_indent()
g.writeln('} catch (e) {') g.writeln('} catch (e) {')
g.writeln(' if (e instanceof BreakException) { break; }') g.writeln(' if (e instanceof BreakException) { break; }')
g.writeln(' else if (e instanceof ContinueException) { continue; }') g.writeln(' else if (e instanceof ContinueException) { continue; }')
@ -1769,12 +1786,16 @@ fn (mut g JsGen) gen_for_stmt(it ast.ForStmt) {
g.expr(it.cond) g.expr(it.cond)
} }
g.writeln(') {') g.writeln(') {')
g.inc_indent()
g.writeln('try { ') g.writeln('try { ')
g.inc_indent()
g.stmts(it.stmts) g.stmts(it.stmts)
g.dec_indent()
g.writeln('} catch (e) {') g.writeln('} catch (e) {')
g.writeln(' if (e instanceof BreakException) { break; }') g.writeln(' if (e instanceof BreakException) { break; }')
g.writeln(' else if (e instanceof ContinueException) { continue; }') g.writeln(' else if (e instanceof ContinueException) { continue; }')
g.writeln(' else { throw e; } }') g.writeln(' else { throw e; } }')
g.dec_indent()
g.writeln('}') g.writeln('}')
} }
@ -1823,10 +1844,18 @@ fn (mut g JsGen) gen_return_stmt(it ast.Return) {
fn_return_is_optional := g.fn_decl.return_type.has_flag(.optional) fn_return_is_optional := g.fn_decl.return_type.has_flag(.optional)
if node.exprs.len == 0 { if node.exprs.len == 0 {
if fn_return_is_optional { if fn_return_is_optional {
if g.inside_or {
g.writeln('throw new ReturnException({state: new int(0)});')
} else {
g.writeln('return {state: new int(0)}') g.writeln('return {state: new int(0)}')
}
} else {
if g.inside_or {
g.writeln('throw new ReturnException(undefined);')
} else { } else {
g.writeln('return;') g.writeln('return;')
} }
}
return return
} }
@ -1841,8 +1870,15 @@ fn (mut g JsGen) gen_return_stmt(it ast.Return) {
g.writeln('return $test_error_var;') g.writeln('return $test_error_var;')
return return
} }
if !g.inside_or {
g.write('return ') g.write('return ')
} else {
g.write('throw new ReturnException(')
}
g.gen_optional_error(it.exprs[0]) g.gen_optional_error(it.exprs[0])
if g.inside_or {
g.writeln(')')
}
g.writeln(';') g.writeln(';')
return return
} }
@ -1860,15 +1896,26 @@ fn (mut g JsGen) gen_return_stmt(it ast.Return) {
g.gen_array_init_values(it.exprs) g.gen_array_init_values(it.exprs)
} }
g.writeln('') g.writeln('')
if g.inside_or {
g.write('throw new ReturnException($tmp);')
} else {
g.write('return $tmp;') g.write('return $tmp;')
}
return return
} }
if !g.inside_or {
g.write('return ') g.write('return ')
} else {
g.write('throw new ReturnException(')
}
if it.exprs.len == 1 { if it.exprs.len == 1 {
g.expr(it.exprs[0]) g.expr(it.exprs[0])
} else { // Multi return } else { // Multi return
g.gen_array_init_values(it.exprs) g.gen_array_init_values(it.exprs)
} }
if g.inside_or {
g.writeln(')')
}
g.writeln(';') g.writeln(';')
} }
@ -2808,11 +2855,13 @@ fn (mut g JsGen) gen_if_expr(node ast.IfExpr) {
} }
} }
} }
g.inc_indent()
if needs_tmp_var { if needs_tmp_var {
g.stmts_with_tmp_var(branch.stmts, tmp) g.stmts_with_tmp_var(branch.stmts, tmp)
} else { } else {
g.stmts(branch.stmts) g.stmts(branch.stmts)
} }
g.dec_indent()
} }
if node.branches.len > 0 { if node.branches.len > 0 {
g.writeln('}') g.writeln('}')