jerryscript-project / jerryscript

Ultra-lightweight JavaScript engine for the Internet of Things.
https://jerryscript.net
Apache License 2.0
6.92k stars 671 forks source link

disable assert implementation #28

Closed seanshpark closed 9 years ago

seanshpark commented 9 years ago

'assert' function itself is not included in ECMA spec but is included in JerryScript, opcodes-dumper.cpp programs embedding JerryScript will implement 'assert', including IoT.js. need some compile time option to disable internal 'assert' and maybe others like 'print'

egavrin commented 9 years ago

@seanshpark I agree that functions like print should be removed from the core and implemented separately using Emd. API, but what is the reason to disable assert?

Anyway, it can be implemented like that (http://stackoverflow.com/a/15313435/230855):

function assert(condition, message) {
    if (!condition) {
        message = message || "Assertion failed";
        if (typeof Error !== "undefined") {
            throw new Error(message);
        }
        throw message; // Fallback
    }
}

BTW, I'd like to improve with smth like the following (http://wiki.ecmascript.org/doku.php?id=strawman:assert):

assert(x === 12); // throws an AssertionError if false, with a default error message
assert(x === 12, "I think it should be twelve") // throws an AssertionError if false, with the given error message
seanshpark commented 9 years ago

@egavrin , but what is the reason to disable assert? is that test codes will have it's implementation of assert() as you mentioned. I was planning to prepare this in iotjs.

ILyoan commented 9 years ago

Looks like Jerry engine snatches assert() expression for example, following program runs unexpectedly.

function assert(value) {
  print(value);
}
assert(0);
galpeter commented 9 years ago

IMHO: That is because the call to assert is treated as a special intrinsic call, thus the defined assert function will not be called.

egavrin commented 9 years ago

Fixed.

seanshpark commented 9 years ago

Thank you!