Skip to content

Commit

Permalink
convert NativeMath and NativeJSON into a Lambda based ScriptableObject
Browse files Browse the repository at this point in the history
and add the toStringTag property
  • Loading branch information
rbri authored and gbrail committed Sep 9, 2023
1 parent 90ac409 commit 1485f1e
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 777 deletions.
135 changes: 36 additions & 99 deletions src/org/mozilla/javascript/NativeJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,30 @@
*
* @author Matthew Crumley, Raphael Speyer
*/
public final class NativeJSON extends IdScriptableObject {
public final class NativeJSON extends ScriptableObject {
private static final long serialVersionUID = -4567599697595654984L;

private static final Object JSON_TAG = "JSON";
private static final String JSON_TAG = "JSON";

private static final int MAX_STRINGIFY_GAP_LENGTH = 10;

static void init(Scriptable scope, boolean sealed) {
NativeJSON obj = new NativeJSON();
obj.activatePrototypeMap(MAX_ID);
obj.setPrototype(getObjectPrototype(scope));
obj.setParentScope(scope);
NativeJSON json = new NativeJSON();
json.setPrototype(getObjectPrototype(scope));
json.setParentScope(scope);

json.defineProperty(scope, "parse", 2, NativeJSON::parse, DONTENUM, DONTENUM | READONLY);
json.defineProperty(
scope, "stringify", 3, NativeJSON::stringify, DONTENUM, DONTENUM | READONLY);

json.defineProperty("toSource", "JSON", DONTENUM | READONLY | PERMANENT);

json.defineProperty(SymbolKey.TO_STRING_TAG, JSON_TAG, DONTENUM | READONLY);

ScriptableObject.defineProperty(scope, JSON_TAG, json, DONTENUM);
if (sealed) {
obj.sealObject();
json.sealObject();
}
ScriptableObject.defineProperty(scope, "JSON", obj, ScriptableObject.DONTENUM);
}

private NativeJSON() {}
Expand All @@ -48,77 +56,32 @@ public String getClassName() {
return "JSON";
}

@Override
protected void initPrototypeId(int id) {
if (id <= LAST_METHOD_ID) {
String name;
int arity;
switch (id) {
case Id_toSource:
arity = 0;
name = "toSource";
break;
case Id_parse:
arity = 2;
name = "parse";
break;
case Id_stringify:
arity = 3;
name = "stringify";
break;
default:
throw new IllegalStateException(String.valueOf(id));
}
initPrototypeMethod(JSON_TAG, id, name, arity);
} else {
throw new IllegalStateException(String.valueOf(id));
private static Object parse(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
String jtext = ScriptRuntime.toString(args, 0);
Object reviver = null;
if (args.length > 1) {
reviver = args[1];
}
if (reviver instanceof Callable) {
return parse(cx, scope, jtext, (Callable) reviver);
}
return parse(cx, scope, jtext);
}

@Override
public Object execIdCall(
IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
if (!f.hasTag(JSON_TAG)) {
return super.execIdCall(f, cx, scope, thisObj, args);
}
int methodId = f.methodId();
switch (methodId) {
case Id_toSource:
return "JSON";

case Id_parse:
{
String jtext = ScriptRuntime.toString(args, 0);
Object reviver = null;
if (args.length > 1) {
reviver = args[1];
}
if (reviver instanceof Callable) {
return parse(cx, scope, jtext, (Callable) reviver);
}
return parse(cx, scope, jtext);
}

case Id_stringify:
{
Object value = Undefined.instance, replacer = null, space = null;

if (args.length > 0) {
value = args[0];
if (args.length > 1) {
replacer = args[1];
if (args.length > 2) {
space = args[2];
}
}
}
private static Object stringify(
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Object value = Undefined.instance, replacer = null, space = null;

return stringify(cx, scope, value, replacer, space);
if (args.length > 0) {
value = args[0];
if (args.length > 1) {
replacer = args[1];
if (args.length > 2) {
space = args[2];
}

default:
throw new IllegalStateException(String.valueOf(methodId));
}
}
return stringify(cx, scope, value, replacer, space);
}

private static Object parse(Context cx, Scriptable scope, String jtext) {
Expand Down Expand Up @@ -620,30 +583,4 @@ private static boolean isObjectArrayLike(Object o) {
}
return false;
}

@Override
protected int findPrototypeId(String s) {
int id;
switch (s) {
case "toSource":
id = Id_toSource;
break;
case "parse":
id = Id_parse;
break;
case "stringify":
id = Id_stringify;
break;
default:
id = 0;
break;
}
return id;
}

private static final int Id_toSource = 1,
Id_parse = 2,
Id_stringify = 3,
LAST_METHOD_ID = 3,
MAX_ID = 3;
}
Loading

0 comments on commit 1485f1e

Please sign in to comment.