Commit Graph

716 Commits

Author SHA1 Message Date
Tor Andersson
88f6d86b6c Add js_isbooleanobject and js_isdateobject functions. 2022-05-25 16:33:53 +02:00
Tor Andersson
f5b3c703e1 Issue #161: Cope with empty programs in mujs-pp. 2022-05-17 15:57:55 +02:00
Tor Andersson
910acc807c Issue #161: Don't fclose a FILE that is NULL. 2022-05-17 15:53:30 +02:00
Tor Andersson
160ae29578 Issue #162: Check stack overflow during regexp compilation.
Only bother checking during the first compilation pass that counts
the size of the program.
2022-05-17 15:32:16 +02:00
Tor Andersson
db110ea88e Bug 705052: Don't use private STACK/TOP macros in jsstate.c
These convenience macros are defined and used in jsrun.c, and should not
be used in other files.
2022-03-16 11:34:18 +01:00
Tor Andersson
76e400fb67 Add "console" object to mujs shell. 2022-03-10 17:42:25 +01:00
Tor Andersson
434ae67268 Issue #156: Fix check for duplicate formal parameters when strict.
Duplicate var and function declarations are still allowed.
2022-03-10 17:42:00 +01:00
Tor Andersson
4586e81832 Some minor optimizations to Ap_join. 2022-02-23 14:58:49 +01:00
Avi Halachmi (:avih)
a2b628c9cb array join: avoid strcat, speedup from O(N^2) to O(N)
Previously, each iteration (except the 1st) did this amount of calls:
  2x strlen(result-so-far) + 2x strlen(element) + strlen(sep)
Where except one strlen(element) they're implicit inside strcat, and
of sizes which we already know.

Now each iteration does one strlen(element), and no strcat.

The big speedup is avoiding strlen of the result so far (twice) on
each iteration - O(N^2), but the other extra 2x strlen can add up too.

Join of an array of 2000 strings of 80 chars each:
  Windows: before: 80ms, after: 2ms
  Linux:   before: 20ms, after: 2ms
Measured using Date.now()
2022-02-16 12:31:25 +02:00
Tor Andersson
dd0a0972b4 Add JS_VERSION_MAJOR/MINOR/PATCH defines to mujs.h
A macro JS_CHECKVERSION(major, minor, patch) can be used to test the
version if your code depends on API features added in a given version.

#if JS_CHECKVERSION(1, 2, 0)
    ... use new API ...
#else
    ... don't use new API ...
#endif
1.2.0
2021-12-08 12:56:12 +01:00
Tor Andersson
1780d0ea73 Bug 704756: Don't trust function.length property!
Calling js_call with n < 0 led to us popping a negative number of items
from the stack, which could make us miss the stack size check.

Sanitize all uses of function.length in Function.prototype.apply and
Function.prototype.bind.
2021-12-08 12:56:12 +01:00
Tor Andersson
70bd7ea61c Minor cleanups. 2021-12-06 16:41:36 +01:00
Tor Andersson
78e56b7854 Bug 704748: Save original object in stack slot for returning after constructor.
The object in the 'this' slot may be overwritten if the constructor converts
it to a primitive value. Save the original object in an explicit stack slot
to keep it safe for returning afterwards.
2021-12-06 16:40:44 +01:00
Tor Andersson
eed8a67a49 Bug 704750 and 704751: Save transient js_toobject in stack slot.
Prevent use-after-free if GC is triggered while a js_toobject is held.

This is the same stack clobbering approach used for js_tostring, etc.
2021-12-06 15:42:08 +01:00
李通洲
70a299c76b Build shared libs as dylib on macOS 2021-12-06 11:58:41 +01:00
Tor Andersson
df8559e7bd Bug 704749: Clear jump list after patching jump addresses.
Since we can emit a statement multiple times when compiling try/finally
we have to use a new patch list for each instance.
2021-12-06 11:48:32 +01:00
Tor Andersson
90a63426ee Issue #152: Work around GCC compiler bug introduced in 2015.
See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103052 for details.
2021-11-05 12:59:02 +01:00
Tor Andersson
e7ba87678f Silence harmless GCC warnings. 2021-11-05 12:59:02 +01:00
Tor Andersson
3bd234c685 Math.random: Use Lehmer LCG instead of borrowing the hack from musl.
For simplicity of implementation, use the minimal standard generator
described in "Random Number Generators: Good ones are hard to find"
by Park & Miller (ACM 1988, Volume 31, Number 10).
2021-11-05 12:59:02 +01:00
Tor Andersson
fe8cac61e3 Add user data to C functions with extended constructor.
Accessible from C with js_currentfunctiondata(J).
2021-11-04 13:08:51 +01:00
Tor Andersson
daa2241087 Increase default string limit to 256M. 2021-09-20 14:08:39 +02:00
Tor Andersson
b06a5e9b02 Bug 704238: Limit max string lengths.
Check string length when creating strings to not exceed a maximum,
so we avoid integer overflows when concatenating strings.

The string limit must be small enough that we'll not integer overflow
in one concatenation (A + B + 1 must not overflow while still
exceeding the string limit).

Set the limit to 64KB for now.

If we need 2GB strings then we will have to use double or int64 variables
when calculating string lengths.
2021-09-07 18:02:28 +02:00
Tor Andersson
453d28fcc0 Don't use realloc(p, 0) to mean free() in default regex allocator. 2021-08-01 13:58:34 +02:00
Tor Andersson
9dd965b679 Add special error handling for safe 'protected' functions. 2021-07-23 16:23:08 +02:00
Tor Andersson
822061539a Fix leaks if js_try runs out of exception stack space.
Since js_try can throw to its surrounding exception scope if it runs out
of space in the exception stack, we need to make sure to allocate
resources guarded by the try inside the try not outside it.
2021-07-23 12:43:15 +02:00
Tor Andersson
a9d88e54af Handle try stack errors like stack overflow errors.
Don't create a new object, because that may cause a cascade of
other errors since we're at the limit.
2021-07-23 11:42:35 +02:00
Tor Andersson
2a1804ea26 Generate new tables for isalpha/toupper/tolower from UnicodeDate.txt 2021-07-20 15:01:53 +02:00
Tor Andersson
1b8aae1d3c Restore missing copyright headers! 2021-07-20 11:24:40 +02:00
Tor Andersson
d4a599edea Issue 150: Fix regexp.exec bugs. 2021-07-08 18:13:35 +02:00
Tor Andersson
c3ce563aa9 Debug print whether a function is strict. 2021-07-08 18:13:12 +02:00
Tor Andersson
c3715ce3db Fix error in Array.prototype.toString(). 1.1.3 2021-06-08 13:41:51 +02:00
Robin Watts
6625d1ea02 Disable const warnings in Visual Studio.
Visual Studio is overzealous (i.e. wrong) in many of its const
warnings.
2021-06-03 12:45:04 +01:00
Tor Andersson
7ef066a3bb Fix use-after-free in regexp source property access.
The underlying string of the "source" property of a regular expression
object can be freed if the regexp is garbage collected.

This could lead to a use-after-free, because the accessor incorrectly
assumed that the regexp source was an interned (thus never freed) string.
Fix this by calling js_pushstring instead of the faster but unsafe
js_pushliteral.

Many thanks to Connor Nelson for spotting this!
1.1.2
2021-04-23 11:13:50 +02:00
Tor Andersson
dbb86fcd8b Call "join" property instead of hardcoding Ap_join in Ap_toString. 2021-04-23 11:13:50 +02:00
Tor Andersson
e38bff1f68 ... and leave a bit of margin. 2021-04-21 15:07:16 +02:00
Tor Andersson
833b6f1672 Issue #148: Check for overflow when reading floating point exponent.
GCC with -O2 optimizes away the if(exp<-maxExponent) branch completely,
so we don't end up with the expected '512' value for overflowing
exponents. Limit the exponent parsing to MAX_INT instead to prevent
signed overflow from tripping up over-eager optimizing compilers.
2021-04-21 12:25:48 +02:00
Avi Halachmi (:avih)
292415b625 test262: improve quoting, handle spaces at files/arguments
- Don't coalesce IFS nor interpret backslash at the output of `find'.
- The argument of -m and -l can now be arbitrary.
- The TC39 test262 path and/or files can now be arbitrary.

Previously any of those broke on IFS chars at the value.
Now the only issue is (unlikely) newline at filenames - from `find'.
2021-04-20 00:14:17 +02:00
Avi Halachmi (:avih)
d5f6b3b66e test262: remove incorrect shift
It remained accidentally when the script changed from custom
arguments parsing to using getopts.
2021-04-20 00:14:17 +02:00
Avi Halachmi (:avih)
857ffd39b0 tools: add test262 and harness
tools: add test262-harness.js to run a single test

Usage: mujs <this-file> -- [-f] [-l file1.js -l ...] suit-root test-file
-f: print full paths/stacktraces if possible
-l: load a js file after the harness and before the test (to override things)

tools: add test-262 launcher

It can run the entire suite or just a sub-folder or one file, and by
default it skips tests which are known to crash/hang.

test262-harness: @negative: match regex if exists

Some @negative tests add a regex which needs to match the error.
This wasn't tested, and now it is. This results in few more failures.

The actual string to compare is not documented, but it appears to be
err.message for plain Error(..) where the message is always compared
to "NotEarlyError" (equals/different), and err.name for anything else.

test262 launcher: minor improvements and custom mujs path

- Use getopts instead of custom arguments parsing
- Support -m path/to/mujs
- Change -s (skip bad tests) to -b, and add -B to run only bad tests
  (useful when trying to update the known bad tests list).
- Combine stderr to stdout on a per-test basis (mujs function warnings)
- Exit with error if failures > 0

test262: remove 5 tests which no longer crash/hang

test262 and harness: support -s to print failed source

test262 and harness: convert spaces to tabs
2021-04-19 12:57:43 +02:00
Tor Andersson
e00c9ba79c Don't call realloc with size=0 to free data.
Newer versions of the C spec and POSIX have changed the behavior of
realloc called with size 0 to be implementation defined.
2021-04-17 21:42:49 +02:00
Connor Nelson
6d1404397f Prevent negative table indexing in js_strtol 1.1.1 2021-04-12 19:22:04 -07:00
Connor Nelson
f93d24539b Fix js_strtol 2021-03-27 11:12:05 +01:00
Sebastian Rasmussen
72e95a48e3 Add user.make for persistent custom settings. 2021-03-26 16:41:43 +01:00
Sebastian Rasmussen
d1160c7933 Improve gitignore. 2021-03-26 16:41:43 +01:00
Tor Andersson
3d29cd2f2f Issue #135: Expose type of value as an enum with js_type().
This matches the values used by the 'typeof' operator.
2021-03-26 15:47:35 +01:00
Tor Andersson
33ffe6efeb Inline doubles and interned string pointers in the byte code.
Avoid linearly searched per function string and number tables.
2021-03-26 14:35:14 +01:00
Tor Andersson
06a6f9fb11 Issue #120: Optimize array construction bytecode.
Use a specialized array initializer that pushes values to the end
of the array instead of using a lot of setprop. This avoids the need
to create a lot of number constants for the array indices.
2021-03-26 12:05:35 +01:00
Tor Andersson
4c7f6be433 Issue #139: Parse integers with floats to support large numbers.
Add a js_strtol which parses integers with bases 2..36 using simple
double precision arithmetic with no overflow checks.
2021-03-25 14:43:07 +01:00
Tor Andersson
1616c18484 Some Makefile tweaks.
Change default optimization flag to -O2.

Use XCFLAGS and XCPPFLAGS to add to the definitions:

  make XCPPFLAGS=-I/foo/bar XCFLAGS=-mfoo-bar

Set CFLAGS on the make command line if you want to override completely:

  make CFLAGS="-O3 -m32"

If you want to override them with the system environment variables:

  make CFLAGS="$CFLAGS"
2021-03-25 14:43:07 +01:00
isRyven
625542e463 fix split doesn't convert context to string if no argument is passed 2021-03-24 15:43:04 +01:00