no more implicit returning in programs and mdouels
This commit is contained in:
2
audit.ce
2
audit.ce
@@ -89,5 +89,3 @@ if (length(all_unresolved) > 0) summary = summary + ", " + text(length(all_unres
|
||||
log.console(summary)
|
||||
}
|
||||
run()
|
||||
|
||||
$stop()
|
||||
|
||||
22
mcode.cm
22
mcode.cm
@@ -3103,7 +3103,6 @@ var mcode = function(ast) {
|
||||
var local_slot = 0
|
||||
var dest = 0
|
||||
var statements = ast.statements
|
||||
var last_expr_slot = -1
|
||||
var stmt = null
|
||||
var kind = null
|
||||
var null_slot = 0
|
||||
@@ -3170,21 +3169,18 @@ var mcode = function(ast) {
|
||||
kind = stmt.kind
|
||||
if (kind != null) {
|
||||
if (kind == "call") {
|
||||
last_expr_slot = gen_expr(stmt.expression, -1)
|
||||
gen_expr(stmt.expression, -1)
|
||||
} else if (kind == "return" || kind == "disrupt" ||
|
||||
kind == "break" || kind == "continue") {
|
||||
gen_statement(stmt)
|
||||
last_expr_slot = -1
|
||||
} else if (kind == "var" || kind == "def" ||
|
||||
kind == "break" || kind == "continue" ||
|
||||
kind == "var" || kind == "def" ||
|
||||
kind == "var_list" || kind == "def_list" ||
|
||||
kind == "function" || kind == "block" ||
|
||||
kind == "if" || kind == "while" ||
|
||||
kind == "do" || kind == "for" ||
|
||||
kind == "switch") {
|
||||
gen_statement(stmt)
|
||||
last_expr_slot = -1
|
||||
} else {
|
||||
last_expr_slot = gen_expr(stmt, -1)
|
||||
gen_expr(stmt, -1)
|
||||
}
|
||||
} else {
|
||||
gen_statement(stmt)
|
||||
@@ -3192,13 +3188,9 @@ var mcode = function(ast) {
|
||||
_i = _i + 1
|
||||
}
|
||||
|
||||
if (last_expr_slot >= 0) {
|
||||
emit_1("return", last_expr_slot)
|
||||
} else {
|
||||
null_slot = alloc_slot()
|
||||
emit_1("null", null_slot)
|
||||
emit_1("return", null_slot)
|
||||
}
|
||||
null_slot = alloc_slot()
|
||||
emit_1("null", null_slot)
|
||||
emit_1("return", null_slot)
|
||||
|
||||
result = {}
|
||||
result.name = filename != null ? filename : "<eval>"
|
||||
|
||||
53
vm_suite.ce
53
vm_suite.ce
@@ -7650,6 +7650,59 @@ run("string index in text() call", function() {
|
||||
assert_eq(s[0] + sub, "$abc", "reassemble from index + substring")
|
||||
})
|
||||
|
||||
// ============================================================================
|
||||
// NO IMPLICIT RETURN
|
||||
// ============================================================================
|
||||
|
||||
run("function trailing call no implicit return", function() {
|
||||
var fn = function() { text(42) }
|
||||
assert_eq(fn(), null, "trailing call should not implicitly return")
|
||||
})
|
||||
|
||||
run("function trailing expression no implicit return", function() {
|
||||
var fn = function() { var x = 1 + 2 }
|
||||
assert_eq(fn(), null, "trailing expression should not implicitly return")
|
||||
})
|
||||
|
||||
run("function trailing arithmetic no implicit return", function() {
|
||||
var fn = function() {
|
||||
var x = 10
|
||||
x + 5
|
||||
}
|
||||
assert_eq(fn(), null, "bare arithmetic should not implicitly return")
|
||||
})
|
||||
|
||||
run("function explicit return still works", function() {
|
||||
var fn = function() { return 42 }
|
||||
assert_eq(fn(), 42, "explicit return should work")
|
||||
})
|
||||
|
||||
run("arrow trailing call no implicit return", function() {
|
||||
var fn = (x) => { text(x) }
|
||||
assert_eq(fn(42), null, "arrow trailing call should not implicitly return")
|
||||
})
|
||||
|
||||
run("arrow trailing arithmetic no implicit return", function() {
|
||||
var fn = (x) => {
|
||||
var y = x + 1
|
||||
y * 2
|
||||
}
|
||||
assert_eq(fn(5), null, "arrow bare arithmetic should not implicitly return")
|
||||
})
|
||||
|
||||
run("arrow explicit return still works", function() {
|
||||
var fn = (x) => { return x * 2 }
|
||||
assert_eq(fn(5), 10, "arrow explicit return should work")
|
||||
})
|
||||
|
||||
run("function explicit return before trailing expr", function() {
|
||||
var fn = function() {
|
||||
return 7
|
||||
text(99)
|
||||
}
|
||||
assert_eq(fn(), 7, "explicit return should take precedence")
|
||||
})
|
||||
|
||||
// ============================================================================
|
||||
// SUMMARY
|
||||
// ============================================================================
|
||||
|
||||
Reference in New Issue
Block a user