shithub: libmujs

Download patch

ref: d9ed73fd717ebbefe5595d139a133b762cea4e92
parent: d05415b053599b955ab4a4859f19fab2de4c01d1
author: Tor Andersson <tor.andersson@artifex.com>
date: Wed Jan 7 11:54:08 EST 2015

strict mode: 'this' is undefined for normal function calls.

Global (script) code still uses the global object as 'this'.
Eval still uses the calling context's 'this'.

--- a/jsarray.c
+++ b/jsarray.c
@@ -265,7 +265,7 @@
 
 		if (hasfn) {
 			js_copy(J, 1); /* copy function */
-			js_pushglobal(J); /* set this object */
+			js_pushundefinedthis(J); /* set this object */
 			js_copy(J, -4); /* copy x */
 			js_copy(J, -4); /* copy y */
 			js_call(J, 2);
--- a/jscompile.c
+++ b/jscompile.c
@@ -516,7 +516,7 @@
 		/* fall through */
 	default:
 		cexp(J, F, fun);
-		emit(J, F, OP_GLOBAL);
+		emit(J, F, J->strict ? OP_UNDEF : OP_GLOBAL);
 		break;
 	}
 	n = cargs(J, F, args);
--- a/jsi.h
+++ b/jsi.h
@@ -92,6 +92,8 @@
 void js_dup1rot3(js_State *J);
 void js_dup1rot4(js_State *J);
 
+void js_pushundefinedthis(js_State *J); /* push 'global' if non-strict, undefined if strict */
+
 void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text);
 
 void js_trap(js_State *J, int pc); /* dump stack and environment to stdout */
--- a/jsrun.c
+++ b/jsrun.c
@@ -153,6 +153,14 @@
 	js_pushobject(J, J->G);
 }
 
+void js_pushundefinedthis(js_State *J)
+{
+	if (J->strict)
+		js_pushundefined(J);
+	else
+		js_pushobject(J, J->G);
+}
+
 void js_currentfunction(js_State *J)
 {
 	CHECKSTACK(1);
--- a/jsstring.c
+++ b/jsstring.c
@@ -398,7 +398,7 @@
 
 	if (js_iscallable(J, 2)) {
 		js_copy(J, 2);
-		js_pushglobal(J);
+		js_pushundefinedthis(J);
 		for (x = 0; m.sub[x].sp; ++x) /* arg 0..x: substring and subexps that matched */
 			js_pushlstring(J, m.sub[x].sp, m.sub[x].ep - m.sub[x].sp);
 		js_pushnumber(J, s - source); /* arg x+2: offset within search string */
@@ -492,7 +492,7 @@
 
 	if (js_iscallable(J, 2)) {
 		js_copy(J, 2);
-		js_pushglobal(J);
+		js_pushundefinedthis(J);
 		js_pushlstring(J, s, n); /* arg 1: substring that matched */
 		js_pushnumber(J, s - source); /* arg 2: offset within search string */
 		js_copy(J, 0); /* arg 3: search string */