@Override public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { if (!f.hasTag(CLASS_NAME)) { return super.execIdCall(f, cx, scope, thisObj, args); } int id = f.methodId(); switch (id) { case ConstructorId_isView: return (isArg(args, 0) && (args[0] instanceof NativeArrayBufferView)); case Id_constructor: int length = isArg(args, 0) ? ScriptRuntime.toInt32(args[0]) : 0; return new NativeArrayBuffer(length); case Id_slice: NativeArrayBuffer self = realThis(thisObj, f); int start = isArg(args, 0) ? ScriptRuntime.toInt32(args[0]) : 0; int end = isArg(args, 1) ? ScriptRuntime.toInt32(args[1]) : self.buffer.length; return self.slice(start, end); } throw new IllegalArgumentException(String.valueOf(id)); }
private void setRange(NativeTypedArrayView v, int off) { if (off >= length) { throw ScriptRuntime.constructError("RangeError", "offset out of range"); } if (v.length > (length - off)) { throw ScriptRuntime.constructError("RangeError", "source array too long"); } if (v.arrayBuffer == arrayBuffer) { // Copy to temporary space first, as per spec, to avoid messing up overlapping copies Object[] tmp = new Object[v.length]; for (int i = 0; i < v.length; i++) { tmp[i] = v.js_get(i); } for (int i = 0; i < v.length; i++) { js_set(i + off, tmp[i]); } } else { for (int i = 0; i < v.length; i++) { js_set(i + off, v.js_get(i)); } } }
private Object js_getInt(int bytes, boolean signed, Object[] args) { checkOffset(args, 0); int pos = ScriptRuntime.toInt32(args[0]); rangeCheck(pos, bytes); boolean littleEndian = (isArg(args, 1) && (bytes > 1) && ScriptRuntime.toBoolean(args[1])); switch (bytes) { case 1: return (signed ? ByteIo.readInt8(arrayBuffer.buffer, offset + pos) : ByteIo.readUint8(arrayBuffer.buffer, offset + pos)); case 2: return (signed ? ByteIo.readInt16(arrayBuffer.buffer, offset + pos, littleEndian) : ByteIo.readUint16(arrayBuffer.buffer, offset + pos, littleEndian)); case 4: return (signed ? ByteIo.readInt32(arrayBuffer.buffer, offset + pos, littleEndian) : ByteIo.readUint32(arrayBuffer.buffer, offset + pos, littleEndian)); default: throw new AssertionError(); } }
private Object js_getFloat(int bytes, Object[] args) { checkOffset(args, 0); int pos = ScriptRuntime.toInt32(args[0]); rangeCheck(pos, bytes); boolean littleEndian = (isArg(args, 1) && (bytes > 1) && ScriptRuntime.toBoolean(args[1])); switch (bytes) { case 4: return ByteIo.readFloat32(arrayBuffer.buffer, offset + pos, littleEndian); case 8: return ByteIo.readFloat64(arrayBuffer.buffer, offset + pos, littleEndian); default: throw new AssertionError(); } }
private void js_setFloat(int bytes, Object[] args) { checkOffset(args, 0); checkValue(args, 1); int pos = ScriptRuntime.toInt32(args[0]); rangeCheck(pos, bytes); boolean littleEndian = (isArg(args, 2) && (bytes > 1) && ScriptRuntime.toBoolean(args[2])); double val = ScriptRuntime.toNumber(args[1]); switch (bytes) { case 4: ByteIo.writeFloat32(arrayBuffer.buffer, offset + pos, val, littleEndian); break; case 8: ByteIo.writeFloat64(arrayBuffer.buffer, offset + pos, val, littleEndian); break; default: throw new AssertionError(); } }
public static int toUint8Clamp(Object arg) { double d = ScriptRuntime.toNumber(arg); if (d <= 0.0) { return 0; } if (d >= 255.0) { return 255; } // Complex rounding behavior -- see 7.1.11 double f = Math.floor(d); if ((f + 0.5) < d) { return (int)(f + 1.0); } if (d < (f + 0.5)) { return (int)f; } if (((int)f % 2) != 0) { return (int)f + 1; } return (int)f; }
public static void init(Context cx, Scriptable scope, boolean sealed) { NativeRegExp proto = new NativeRegExp(); proto.re = compileRE(cx, "", null, false); proto.activatePrototypeMap(MAX_PROTOTYPE_ID); proto.setParentScope(scope); proto.setPrototype(getObjectPrototype(scope)); NativeRegExpCtor ctor = new NativeRegExpCtor(); // Bug #324006: ECMA-262 15.10.6.1 says "The initial value of // RegExp.prototype.constructor is the builtin RegExp constructor." proto.defineProperty("constructor", ctor, ScriptableObject.DONTENUM); ScriptRuntime.setFunctionProtoAndParent(ctor, scope); ctor.setImmunePrototypeProperty(proto); if (sealed) { proto.sealObject(); ctor.sealObject(); } defineProperty(scope, "RegExp", ctor, ScriptableObject.DONTENUM); }
Scriptable compile(Context cx, Scriptable scope, Object[] args) { if (args.length > 0 && args[0] instanceof NativeRegExp) { if (args.length > 1 && args[1] != Undefined.instance) { // report error throw ScriptRuntime.typeError0("msg.bad.regexp.compile"); } NativeRegExp thatObj = (NativeRegExp) args[0]; this.re = thatObj.re; this.lastIndex = thatObj.lastIndex; return this; } String s = args.length == 0 || args[0] instanceof Undefined ? "" : escapeRegExp(args[0]); String global = args.length > 1 && args[1] != Undefined.instance ? ScriptRuntime.toString(args[1]) : null; this.re = compileRE(cx, s, global, false); this.lastIndex = 0d; return this; }
private static String escapeRegExp(Object src) { String s = ScriptRuntime.toString(src); // Escape any naked slashes in regexp source, see bug #510265 StringBuilder sb = null; // instantiated only if necessary int start = 0; int slash = s.indexOf('/'); while (slash > -1) { if (slash == start || s.charAt(slash - 1) != '\\') { if (sb == null) { sb = new StringBuilder(); } sb.append(s, start, slash); sb.append("\\/"); start = slash + 1; } slash = s.indexOf('/', slash + 1); } if (sb != null) { sb.append(s, start, s.length()); s = sb.toString(); } return s; }
private static void addCharacterRangeToCharSet(RECharSet cs, char c1, char c2) { int i; int byteIndex1 = (c1 / 8); int byteIndex2 = (c2 / 8); if ((c2 >= cs.length) || (c1 > c2)) { throw ScriptRuntime.constructError("SyntaxError", "invalid range in character class"); } c1 &= 0x7; c2 &= 0x7; if (byteIndex1 == byteIndex2) { cs.bits[byteIndex1] |= ((0xFF) >> (7 - (c2 - c1))) << c1; } else { cs.bits[byteIndex1] |= 0xFF << c1; for (i = byteIndex1 + 1; i < byteIndex2; i++) cs.bits[i] = (byte)0xFF; cs.bits[byteIndex2] |= (0xFF) >> (7 - c2); } }
@Override protected Object getInstanceIdValue(int id) { switch (id) { case Id_lastIndex: return lastIndex; case Id_source: return new String(re.source); case Id_global: return ScriptRuntime.wrapBoolean((re.flags & JSREG_GLOB) != 0); case Id_ignoreCase: return ScriptRuntime.wrapBoolean((re.flags & JSREG_FOLD) != 0); case Id_multiline: return ScriptRuntime.wrapBoolean((re.flags & JSREG_MULTILINE) != 0); } return super.getInstanceIdValue(id); }
public static void init(Context cx, Scriptable scope, boolean sealed) { NativeRegExp proto = new NativeRegExp(); proto.re = (RECompiled)compileRE(cx, "", null, false); proto.activatePrototypeMap(MAX_PROTOTYPE_ID); proto.setParentScope(scope); proto.setPrototype(getObjectPrototype(scope)); NativeRegExpCtor ctor = new NativeRegExpCtor(); // Bug #324006: ECMA-262 15.10.6.1 says "The initial value of // RegExp.prototype.constructor is the builtin RegExp constructor." proto.defineProperty("constructor", ctor, ScriptableObject.DONTENUM); ScriptRuntime.setFunctionProtoAndParent(ctor, scope); ctor.setImmunePrototypeProperty(proto); if (sealed) { proto.sealObject(); ctor.sealObject(); } defineProperty(scope, "RegExp", ctor, ScriptableObject.DONTENUM); }
Scriptable compile(Context cx, Scriptable scope, Object[] args) { if (args.length > 0 && args[0] instanceof NativeRegExp) { if (args.length > 1 && args[1] != Undefined.instance) { // report error throw ScriptRuntime.typeError0("msg.bad.regexp.compile"); } NativeRegExp thatObj = (NativeRegExp) args[0]; this.re = thatObj.re; this.lastIndex = thatObj.lastIndex; return this; } String s = args.length == 0 ? "" : ScriptRuntime.toString(args[0]); String global = args.length > 1 && args[1] != Undefined.instance ? ScriptRuntime.toString(args[1]) : null; this.re = (RECompiled)compileRE(cx, s, global, false); this.lastIndex = 0; return this; }
@Override protected Object getInstanceIdValue(int id) { switch (id) { case Id_lastIndex: return ScriptRuntime.wrapNumber(lastIndex); case Id_source: return new String(re.source); case Id_global: return ScriptRuntime.wrapBoolean((re.flags & JSREG_GLOB) != 0); case Id_ignoreCase: return ScriptRuntime.wrapBoolean((re.flags & JSREG_FOLD) != 0); case Id_multiline: return ScriptRuntime.wrapBoolean((re.flags & JSREG_MULTILINE) != 0); } return super.getInstanceIdValue(id); }
public void testIt() { final String script = "var fn = function() { return this; }\n" + "fn.apply(1)"; final ContextAction action = new ContextAction() { public Object run(final Context _cx) { final ScriptableObject scope = _cx.initStandardObjects(); final Object result = _cx.evaluateString(scope, script, "test script", 0, null); assertEquals("object", ScriptRuntime.typeof(result)); assertEquals("1", Context.toString(result)); return null; } }; Utils.runWithAllOptimizationLevels(action); }
Scriptable compile(Context cx, Scriptable scope, Object[] args) { if (args.length > 0 && args[0] instanceof NativeRegExp) { if (args.length > 1 && args[1] != Undefined.instance) { // report error throw ScriptRuntime.typeError0("msg.bad.regexp.compile"); } NativeRegExp thatObj = (NativeRegExp) args[0]; this.re = thatObj.re; this.lastIndex = thatObj.lastIndex; return this; } String s = args.length == 0 ? "" : escapeRegExp(args[0]); String global = args.length > 1 && args[1] != Undefined.instance ? ScriptRuntime.toString(args[1]) : null; this.re = compileRE(cx, s, global, false); this.lastIndex = 0; return this; }