org.mozilla.javascript.ScriptRuntime Java Examples

The following examples show how to use org.mozilla.javascript.ScriptRuntime. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NativeRegExp.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: NativeRegExp.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #3
Source File: NativeTypedArrayView.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
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));
        }
    }
}
 
Example #4
Source File: NativeArrayBuffer.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
@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));
}
 
Example #5
Source File: NativeRegExp.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: ApplyOnPrimitiveNumberTest.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #7
Source File: NativeRegExp.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #8
Source File: NativeRegExp.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #9
Source File: NativeRegExp.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #10
Source File: NativeDataView.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
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();
    }
}
 
Example #11
Source File: ApplyOnPrimitiveNumberTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #12
Source File: NativeRegExp.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
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;
}
 
Example #13
Source File: Conversions.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
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;
}
 
Example #14
Source File: NativeRegExp.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
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;
}
 
Example #15
Source File: NativeRegExp.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example #16
Source File: NativeDataView.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
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();
    }
}
 
Example #17
Source File: NativeRegExp.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #18
Source File: NativeDataView.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
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();
    }
}
 
Example #19
Source File: Environment.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void put(String name, Scriptable start, Object value) {
    if (this == thePrototypeInstance)
        super.put(name, start, value);
    else
        System.getProperties().put(name, ScriptRuntime.toString(value));
}
 
Example #20
Source File: Environment.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public Environment(ScriptableObject scope) {
    setParentScope(scope);
    Object ctor = ScriptRuntime.getTopLevelProp(scope, "Environment");
    if (ctor != null && ctor instanceof Scriptable) {
        Scriptable s = (Scriptable) ctor;
        setPrototype((Scriptable) s.get("prototype", s));
    }
}
 
Example #21
Source File: Environment.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public Object get(String name, Scriptable start) {
    if (this == thePrototypeInstance)
        return super.get(name, start);

    String result = System.getProperty(name);
    if (result != null)
        return ScriptRuntime.toObject(getParentScope(), result);
    else
        return Scriptable.NOT_FOUND;
}
 
Example #22
Source File: Environment.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void put(String name, Scriptable start, Object value) {
    if (this == thePrototypeInstance)
        super.put(name, start, value);
    else
        System.getProperties().put(name, ScriptRuntime.toString(value));
}
 
Example #23
Source File: NativeArrayBufferView.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected Object getInstanceIdValue(int id)
{
    switch (id) {
    case Id_buffer:
        return arrayBuffer;
    case Id_byteOffset:
        return ScriptRuntime.wrapInt(offset);
    case Id_byteLength:
        return ScriptRuntime.wrapInt(byteLength);
    default:
        return super.getInstanceIdValue(id);
    }
}
 
Example #24
Source File: NativeRegExp.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Object execSub(Context cx, Scriptable scopeObj,
                       Object[] args, int matchType)
{
    RegExpImpl reImpl = getImpl(cx);
    String str;
    if (args.length == 0) {
        str = reImpl.input;
        if (str == null) {
            reportError("msg.no.re.input.for", toString());
        }
    } else {
        str = ScriptRuntime.toString(args[0]);
    }
    double d = ((re.flags & JSREG_GLOB) != 0) ? lastIndex : 0;

    Object rval;
    if (d < 0 || str.length() < d) {
        lastIndex = 0;
        rval = null;
    }
    else {
        int indexp[] = { (int)d };
        rval = executeRegExp(cx, scopeObj, reImpl, str, indexp, matchType);
        if ((re.flags & JSREG_GLOB) != 0) {
            lastIndex = (rval == null || rval == Undefined.instance)
                        ? 0 : indexp[0];
        }
    }
    return rval;
}
 
Example #25
Source File: XMLName.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public Object set(Context cx, Object value)
{
    if (xmlObject == null) {
        throw ScriptRuntime.undefWriteError(Undefined.instance,
                                            toString(),
                                            value);
    }
    // Assignment to descendants causes parse error on bad reference
    // and this should not be called
    if (isDescendants) throw Kit.codeBug();
    xmlObject.putXMLProperty(this, value);
    return value;
}
 
Example #26
Source File: StringLiteral.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public String toSource(int depth) {
    return new StringBuilder(makeIndent(depth))
            .append(quoteChar)
            .append(ScriptRuntime.escapeString(value, quoteChar))
            .append(quoteChar)
            .toString();
}
 
Example #27
Source File: NativeRegExp.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private static void
addCharacterToCharSet(RECharSet cs, char c)
{
    int byteIndex = (c / 8);
    if (c >= cs.length) {
        throw ScriptRuntime.constructError("SyntaxError",
                "invalid range in character class");
    }
    cs.bits[byteIndex] |= 1 << (c & 0x7);
}
 
Example #28
Source File: Require.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Scriptable executeModuleScript(Context cx, String id,
        Scriptable exports, ModuleScript moduleScript, boolean isMain)
{
    final ScriptableObject moduleObject = (ScriptableObject)cx.newObject(
            nativeScope);
    URI uri = moduleScript.getUri();
    URI base = moduleScript.getBase();
    defineReadOnlyProperty(moduleObject, "id", id);
    if(!sandboxed) {
        defineReadOnlyProperty(moduleObject, "uri", uri.toString());
    }
    final Scriptable executionScope = new ModuleScope(nativeScope, uri, base);
    // Set this so it can access the global JS environment objects.
    // This means we're currently using the "MGN" approach (ModuleScript
    // with Global Natives) as specified here:
    // <http://wiki.commonjs.org/wiki/Modules/ProposalForNativeExtension>
    executionScope.put("exports", executionScope, exports);
    executionScope.put("module", executionScope, moduleObject);
    moduleObject.put("exports", moduleObject, exports);
    install(executionScope);
    if(isMain) {
        defineReadOnlyProperty(this, "main", moduleObject);
    }
    executeOptionalScript(preExec, cx, executionScope);
    moduleScript.getScript().exec(cx, executionScope);
    executeOptionalScript(postExec, cx, executionScope);
    return ScriptRuntime.toObject(nativeScope,
            ScriptableObject.getProperty(moduleObject, "exports"));
}
 
Example #29
Source File: Environment.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public Environment(ScriptableObject scope) {
    setParentScope(scope);
    Object ctor = ScriptRuntime.getTopLevelProp(scope, "Environment");
    if (ctor != null && ctor instanceof Scriptable) {
        Scriptable s = (Scriptable) ctor;
        setPrototype((Scriptable) s.get("prototype", s));
    }
}
 
Example #30
Source File: JsConsole.java    From stetho with MIT License 5 votes vote down vote up
public JsConsole(ScriptableObject scope) {
  setParentScope(scope);
  Object ctor = ScriptRuntime.getTopLevelProp(scope, "Console");
  if (ctor != null && ctor instanceof Scriptable) {
    Scriptable scriptable = (Scriptable) ctor;
    setPrototype((Scriptable) scriptable.get("prototype", scriptable));
  }
}