Functionality of built-in classes is exposed to changes in global object

RESOLVED FIXED

Status

Rhino
Core
RESOLVED FIXED
7 years ago
7 years ago

People

(Reporter: Hannes Wallnoefer, Unassigned)

Tracking

Details

Attachments

(1 attachment)

(Reporter)

Description

7 years ago
Changing the global Object, Array, RegExp, String, Number, etc. properties breaks core functionality of Rhino, including creation of object and array, and regexp literals, and to-object conversion of primitive values.

For most of these internal uses of built-in constructors, ECMA 262 requires objects to be created with the original class prototype object as value of the [[Prototype]] internal property. Since global built-in constructors are defined as writable and deletable, this means we need to be preserved for internal use even if their properties are modified in the global object. 

As far as I can tell this is implemented in all major JS engines (tested with Spidermonkey and V8), and we should do this in Rhino as well.
(Reporter)

Comment 1

7 years ago
Created attachment 516860 [details] [diff] [review]
Adds TopLevel class that enables caching of and direct access to ES built-ins

This patch adds a new TopLevel class that extends IDScriptableObject and is extended by ImporterTopLevel. TopLevel is meant to be used as top-level scope and allows to cache built-in ECMAScript objects such as Object, Array, Function, String etc. 

The patch also adds two methods to ScriptRuntime called newBuiltinObject() and setBuiltinProtoAndParent() that work similar to newObject() and setObjectProtoAndParent() but directly access the cached built-ins in the TopLevel scope. 

If the top-level scope is not an instance of TopLevel or does not have its built-in cache initialized, these methods will fall back to the old behaviour. 

This patch also optimizes JS object creation and toObject() conversion of primitive values by directly using the Java constructor instead of invoking the JavaScript constructor. At the same time it makes Object and Array implementations overridable by making Context.newObject() and Context.newArray() non-final and including the changes proposed in bug #515419.
(Reporter)

Comment 2

7 years ago
Committed the patch.

Checking in src/org/mozilla/javascript/BaseFunction.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/BaseFunction.java,v  <--  BaseFunction.java
new revision: 1.76; previous revision: 1.75
done
Checking in src/org/mozilla/javascript/Context.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/Context.java,v  <--  Context.java
new revision: 1.284; previous revision: 1.283
done
Checking in src/org/mozilla/javascript/ImporterTopLevel.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/ImporterTopLevel.java,v  <--  ImporterTopLevel.java
new revision: 1.44; previous revision: 1.43
done
Checking in src/org/mozilla/javascript/NativeArray.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/NativeArray.java,v  <--  NativeArray.java
new revision: 1.112; previous revision: 1.111
done
Checking in src/org/mozilla/javascript/NativeBoolean.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/NativeBoolean.java,v  <--  NativeBoolean.java
new revision: 1.37; previous revision: 1.36
done
Checking in src/org/mozilla/javascript/NativeJavaArray.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/NativeJavaArray.java,v  <--  NativeJavaArray.java
new revision: 1.29; previous revision: 1.28
done
Checking in src/org/mozilla/javascript/NativeJavaObject.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java,v  <--  NativeJavaObject.java
new revision: 1.91; previous revision: 1.90
done
Checking in src/org/mozilla/javascript/NativeNumber.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/NativeNumber.java,v  <--  NativeNumber.java
new revision: 1.43; previous revision: 1.42
done
Checking in src/org/mozilla/javascript/NativeString.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/NativeString.java,v  <--  NativeString.java
new revision: 1.70; previous revision: 1.69
done
Checking in src/org/mozilla/javascript/NativeWith.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/NativeWith.java,v  <--  NativeWith.java
new revision: 1.31; previous revision: 1.30
done
Checking in src/org/mozilla/javascript/ScriptableObject.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/ScriptableObject.java,v  <--  ScriptableObject.java
new revision: 1.167; previous revision: 1.166
done
Checking in src/org/mozilla/javascript/ScriptRuntime.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java,v  <--  ScriptRuntime.java
new revision: 1.330; previous revision: 1.329
done
RCS file: /cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/TopLevel.java,v
done
Checking in src/org/mozilla/javascript/TopLevel.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/TopLevel.java,v  <--  TopLevel.java
initial revision: 1.1
done
Checking in src/org/mozilla/javascript/regexp/NativeRegExp.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/regexp/NativeRegExp.java,v  <--  NativeRegExp.java
new revision: 1.114; previous revision: 1.113
done
Checking in src/org/mozilla/javascript/regexp/NativeRegExpCtor.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/regexp/NativeRegExpCtor.java,v  <--  NativeRegExpCtor.java
new revision: 1.23; previous revision: 1.22
done
Checking in src/org/mozilla/javascript/regexp/RegExpImpl.java;
/cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/regexp/RegExpImpl.java,v  <--  RegExpImpl.java
new revision: 1.41; previous revision: 1.40
Status: NEW → RESOLVED
Last Resolved: 7 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.