Commit Graph

67 Commits

Author SHA1 Message Date
Tor Andersson
d248b0ce18 Bug 701886: Always create new scope for eval().
Distinguish eval code from script code.
2020-01-02 14:37:05 +01:00
Tor Andersson
69b312d13b Fix coverity issue: memory corruption due to overlapping copy. 2019-09-06 12:15:31 +02:00
Tor Andersson
5de1f97c52 Set appropriate internal class property of arguments object. 2019-03-18 14:11:34 +01:00
Tor Andersson
8b52b57782 Allow js_newobjectx to take null as prototype. 2019-03-18 14:11:34 +01:00
Tor Andersson
d63cf4a906 Throw a TypeError when converting an object without toString and valueOf. 2017-11-15 15:10:33 +01:00
Tor Andersson
a7aae8f84b Clamp to INT_MIN/INT_MAX explicitly in jsV_numbertointeger.
Don't rely on undefined behaviour casting Infinity or out of range
doubles to integer.
2017-07-05 13:31:12 +02:00
Tor Andersson
ebbc191fe1 Add fast integer special case in jsV_numbertostring.
Also fix js_itoa to support signed integers.
2017-05-23 13:15:11 +02:00
Tor Andersson
bfebb7aa5c Use grisu2 algorithm for locale independent dtoa. Use BSD strtod. 2017-05-22 15:54:47 +02:00
Tor Andersson
805e0f0f59 Add js_newobjectx to create an object with a specific prototype object. 2016-09-09 19:56:35 +02:00
Tor Andersson
3e3c382a0c Avoid using 'unsigned int'.
Mixing signed and unsigned ints is just a plain headache.
2016-04-27 16:26:00 +02:00
Tor Andersson
aba6644234 Add delete callback to userdata objects. 2016-03-01 21:52:00 +01:00
Tor Andersson
624f975aae Set the internal length attribute for C constructors. 2016-02-22 20:19:46 +01:00
Tor Andersson
4e6c74b551 Add userdata has/put callbacks for custom properties.
This goes beyond defining accessors, and allows capturing all property
accesses. With these callbacks, things like typed arrays can be
implemented.
2016-01-11 17:02:43 +01:00
Tor Andersson
78ed7e6a99 Use fabs rather than integer abs for taking absolute value of floats. 2015-09-14 11:57:51 +02:00
Tor Andersson
c6d17c7f00 Add finalize callback to userdata objects.
When a userdata object is garbage collected, we should invoke a callback
to client code to let it know that the associated userdata pointer is no
longer in use.
2015-01-14 13:04:15 +01:00
Tor Andersson
05c2ee9e51 Use last resort '[object]' ToPrimitive rather than throwing an error.
Helps to print objects without a toString method, like the global object
and objects created without a prototype.
2015-01-08 15:18:19 +01:00
Tor Andersson
c1edf58629 Apply short string changes in jsvalue.c as well. 2015-01-05 19:38:18 +01:00
Tor Andersson
15ef29e7c0 Use memcpy instead of strcpy when copying short strings. 2015-01-02 13:47:56 +01:00
Tor Andersson
031513b25b Add stack traces to error objects.
Revert 'add context and flag argument to js_newstate' commit.

The context argument just adds clutter. The flag which was intended
for JS_DEBUG and/or JS_STRICT shouldn't be necessary.

js_newcfunction and js_newcconstructor need an extra argument, the
name of the function to use in stack traces.
2014-12-03 01:18:21 +01:00
Tor Andersson
6fa0097d9e Silence GCC warning. 2014-11-29 16:16:14 +01:00
Tor Andersson
6538308087 Add short strings (with data embedded in js_Value).
Allows js_tostring to avoid either interning strings converted from
numbers or creating lots of garbage collected strings.
2014-11-29 16:01:36 +01:00
Tor Andersson
4eae3ec10c Make js_toprimitive (and by consequence js_tonumber/string/...) in-place.
Any coercion between types may overwrite the stack slot with the coerced
value. This is usually not a problem, but if you need to preserve the
original value, you should copy it to another stack slot before running
any functions that may coerce the type (anything involving ToPrimitive).

This change lets us avoid interning the result of toString().

If we later add short strings (embedded in js_Value and js_Property
structs) then we can also avoid creating a garbage collected string or
interning the result of js_tostring on a number.
2014-11-29 13:59:09 +01:00
Tor Andersson
7bb55ad356 Add js_itoa for faster conversion of unsigned int to string. 2014-11-29 12:39:57 +01:00
Tor Andersson
0cbd5326f2 Garbage collect (some) strings.
Separate literal/interned and garbage collected string types in js_Value.

js_pushintern/js_tointern are convenience functions to push/pop strings and
automatically intern them (so that the string pointers are guaranteed to be
stable).

js_pushliteral should push stable strings (either interned or actual literals).

js_pushstring will copy the string into garbage collected memory.

The pointer returned by js_tostring is guaranteed to be stable only for as long as
the stack slot it came from remains untouched.

Some uses will always cause a string to be interned:

 * Using it as a property name.
 * Wrapping it in a new String() object.
 * Strings returned by toString().
	ToPrimitive must not clobber the stack, so the result has to be unrooted.
 * Numbers converted to strings (by js_tostring)
	Likewise, we have nowhere to store the temporary string here.
	Passing in a scratch buffer to js_tostring could help this problem.
	Mostly an issue with array accesses (OP_GETPROP, etc) so an auxiliary
	function and we don't have to clutter the API needlessly.
2014-11-28 17:10:08 +01:00
Tor Andersson
339abac8ab Optimize js_stringtofloat to use integer conversion when possible.
Use strtol conversion when the number is 9 digits or less,
has no decimal point and no exponent.
2014-11-07 16:07:56 +01:00
Tor Andersson
3fe6635426 Differentiate between "global code" and "eval code" scripts.
Add a separate js_loadeval for "eval code" scripts, and let
js_do/loadstring create "global code" scripts.

js_newscript called with the NULL scope is equivalent to 'eval code'.
js_newscript called with the J->GE scape is equivalent to 'global code'.
js_newfunction is created with the lexical scope, i.e. 'function code'.
2014-10-27 16:12:16 +01:00
Tor Andersson
93cc0584df Add portable strtod implementation.
Ignore locale problems with strtod.
2014-09-01 16:56:38 +02:00
Tor Andersson
b37173e52e Silence "no return value"/"unreachable statement" warnings.
clang incorrectly treats a default label as unreachable in a switch on
an enum that has all the cases covered. gcc and msvc complain about
missing return values if there is no default statement.

By making the default case fall through to the JS_TUNDEFINED case we
silence all the compilers and handle corrupt data (an enum that isn't
one of the enumerated values) safely.
2014-03-26 14:27:53 +01:00
Tor Andersson
83dd92f085 Add and use dtoa function from plan9/libfmt. 2014-03-18 15:38:35 +01:00
Tor Andersson
8f87c0a842 Parse 0X1234 as hexadecimal numbers in ToNumber. 2014-03-13 14:41:14 +01:00
Tor Andersson
dc386479da Add and use js_touint16. 2014-03-06 22:33:37 +01:00
Tor Andersson
7bac090c8d Tweak js_strtod. 2014-02-28 17:14:11 +01:00
Tor Andersson
67b33c5a86 Handle malloc failure by throwing exceptions. 2014-02-28 14:24:13 +01:00
Tor Andersson
598de57d76 Fix ToNumber from strings and parseInt and parseFloat.
Follow the spec more strictly, and don't rely on strtod's grammar.

GNU libc strtod accepts 0x prefixes, INF, INFINITY and much more which
we don't want, so pre-filter the string we pass to strtod.
2014-02-27 13:49:22 +01:00
Tor Andersson
32ac46df15 Fix relational equality with NaN. 2014-02-26 15:58:02 +01:00
Tor Andersson
fe723b6f8b Fix unreachable code warnings. 2014-02-25 00:21:47 +01:00
Tor Andersson
c7e9ab0b05 Fix signed/unsigned comparison warnings. 2014-02-24 23:55:58 +01:00
Tor Andersson
5b86924fe1 Fix typo in ToBoolean. 2014-02-13 13:53:36 +01:00
Tor Andersson
60bde0ada4 Improve jsV_stringtonumber.
Tolerate leading and trailing whitespace, and hexadecimals.
Return NAN if there is trailing garbage or errors.
2014-02-11 15:26:29 +01:00
Tor Andersson
8ef83a3cc1 Fix boolean equality tests. 2014-02-11 14:49:49 +01:00
Tor Andersson
56c5249e86 Revert "Handle js_tostring on objects without toString or toValue methods."
This reverts commit 7b37ef2b8a.
2014-02-11 14:28:01 +01:00
Tor Andersson
b219aef882 Add virtual string array index properties for characters. 2014-02-07 16:40:22 +01:00
Tor Andersson
411b8b5b9c Use length property of cfunctions to make sure we have at least N args.
So we don't get temporary stack values that shadow undefined arguments.
2014-02-07 14:24:45 +01:00
Tor Andersson
7b37ef2b8a Handle js_tostring on objects without toString or toValue methods.
Don't throw a type error, just return "[object]" instead.
2014-02-07 13:48:24 +01:00
Tor Andersson
e65764ecf9 Rename DontDelete flag to DontConf. 2014-02-06 16:03:44 +01:00
Tor Andersson
d35cef7df9 Implement getters and setters.
Does not work properly for setters on inherited properties.
2014-02-06 16:03:44 +01:00
Tor Andersson
e8dfb4e624 Rename non-js utility sources to not have the 'js' prefix. 2014-01-28 01:34:41 +01:00
Tor Andersson
489365dcf3 Add some more String functions. 2014-01-27 12:12:19 +01:00
Tor Andersson
90b54a9eec Add userdata class.
js_newuserdata(J, tag, ptr) creates a userdata object, wrapping the ptr.
It takes the prototype for the new object from the top of the stack, just
like js_newcconstructor does.

js_touserdata(J, tag, idx) extracts the userdata pointer. Throws a
TypeError if the object is not a userdata object with a matching tag.

js_isuserdata(J, tag, idx) checks if a value on the stack is a userdata
object with a matching tag.
2014-01-24 17:11:49 +01:00
Tor Andersson
546ee1e6c2 Fix switch test-chain stack leftovers. Fix iterator on null and undefined. 2014-01-24 01:48:24 +01:00