Java Code Examples for org.mozilla.javascript.Context#newArray()

The following examples show how to use org.mozilla.javascript.Context#newArray() . 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: Main.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
static void processFiles(Context cx, String[] args)
{
    // define "arguments" array in the top-level object:
    // need to allocate new array since newArray requires instances
    // of exactly Object[], not ObjectSubclass[]
    Object[] array = new Object[args.length];
    System.arraycopy(args, 0, array, 0, args.length);
    Scriptable argsObj = cx.newArray(global, array);
    global.defineProperty("arguments", argsObj,
                          ScriptableObject.DONTENUM);

    for (String file: fileList) {
        try {
            processSource(cx, file);
        } catch (IOException ioex) {
            Context.reportError(ToolErrorReporter.getMessage(
                    "msg.couldnt.read.source", file, ioex.getMessage()));
            exitCode = EXITCODE_FILE_NOT_FOUND;
        } catch (RhinoException rex) {
            ToolErrorReporter.reportException(
                cx.getErrorReporter(), rex);
            exitCode = EXITCODE_RUNTIME_ERROR;
        } catch (VirtualMachineError ex) {
            // Treat StackOverflow and OutOfMemory as runtime errors
            ex.printStackTrace();
            String msg = ToolErrorReporter.getMessage(
                "msg.uncaughtJSException", ex.toString());
            Context.reportError(msg);
            exitCode = EXITCODE_RUNTIME_ERROR;
        }
    }
}
 
Example 2
Source File: Main.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
static void processFiles(Context cx, String[] args)
{
    // define "arguments" array in the top-level object:
    // need to allocate new array since newArray requires instances
    // of exactly Object[], not ObjectSubclass[]
    Object[] array = new Object[args.length];
    System.arraycopy(args, 0, array, 0, args.length);
    Scriptable argsObj = cx.newArray(global, array);
    global.defineProperty("arguments", argsObj,
                          ScriptableObject.DONTENUM);

    for (String file: fileList) {
        try {
            processSource(cx, file);
        } catch (IOException ioex) {
            Context.reportError(ToolErrorReporter.getMessage(
                    "msg.couldnt.read.source", file, ioex.getMessage()));
            exitCode = EXITCODE_FILE_NOT_FOUND;
        } catch (RhinoException rex) {
            ToolErrorReporter.reportException(
                cx.getErrorReporter(), rex);
            exitCode = EXITCODE_RUNTIME_ERROR;
        } catch (VirtualMachineError ex) {
            // Treat StackOverflow and OutOfMemory as runtime errors
            ex.printStackTrace();
            String msg = ToolErrorReporter.getMessage(
                "msg.uncaughtJSException", ex.toString());
            Context.reportError(msg);
            exitCode = EXITCODE_RUNTIME_ERROR;
        }
    }
}
 
Example 3
Source File: Require.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a new instance of the require() function. Upon constructing it,
 * you will either want to install it in the global (or some other) scope
 * using {@link #install(Scriptable)}, or alternatively, you can load the
 * program's main module using {@link #requireMain(Context, String)} and
 * then act on the main module's exports.
 * @param cx the current context
 * @param nativeScope a scope that provides the standard native JavaScript
 * objects.
 * @param moduleScriptProvider a provider for module scripts
 * @param preExec an optional script that is executed in every module's
 * scope before its module script is run.
 * @param postExec an optional script that is executed in every module's
 * scope after its module script is run.
 * @param sandboxed if set to true, the require function will be sandboxed.
 * This means that it doesn't have the "paths" property, and also that the
 * modules it loads don't export the "module.uri" property.
 */
public Require(Context cx, Scriptable nativeScope,
        ModuleScriptProvider moduleScriptProvider, Script preExec,
        Script postExec, boolean sandboxed) {
    this.moduleScriptProvider = moduleScriptProvider;
    this.nativeScope = nativeScope;
    this.sandboxed = sandboxed;
    this.preExec = preExec;
    this.postExec = postExec;
    setPrototype(ScriptableObject.getFunctionPrototype(nativeScope));
    if(!sandboxed) {
        paths = cx.newArray(nativeScope, 0);
        defineReadOnlyProperty(this, "paths", paths);
    }
    else {
        paths = null;
    }
}
 
Example 4
Source File: Require.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a new instance of the require() function. Upon constructing it,
 * you will either want to install it in the global (or some other) scope
 * using {@link #install(Scriptable)}, or alternatively, you can load the
 * program's main module using {@link #requireMain(Context, String)} and
 * then act on the main module's exports.
 * @param cx the current context
 * @param nativeScope a scope that provides the standard native JavaScript
 * objects.
 * @param moduleScriptProvider a provider for module scripts
 * @param preExec an optional script that is executed in every module's
 * scope before its module script is run.
 * @param postExec an optional script that is executed in every module's
 * scope after its module script is run.
 * @param sandboxed if set to true, the require function will be sandboxed.
 * This means that it doesn't have the "paths" property, and also that the
 * modules it loads don't export the "module.uri" property.
 */
public Require(Context cx, Scriptable nativeScope,
        ModuleScriptProvider moduleScriptProvider, Script preExec,
        Script postExec, boolean sandboxed) {
    this.moduleScriptProvider = moduleScriptProvider;
    this.nativeScope = nativeScope;
    this.sandboxed = sandboxed;
    this.preExec = preExec;
    this.postExec = postExec;
    setPrototype(ScriptableObject.getFunctionPrototype(nativeScope));
    if(!sandboxed) {
        paths = cx.newArray(nativeScope, 0);
        defineReadOnlyProperty(this, "paths", paths);
    }
    else {
        paths = null;
    }
}
 
Example 5
Source File: DataFrameAdapter.java    From joinery with GNU General Public License v3.0 4 votes vote down vote up
public static Scriptable jsFunction_toArray(final Context ctx, final Scriptable object, final Object[] args, final Function func) {
    return ctx.newArray(object, cast(object).df.toArray());
}
 
Example 6
Source File: DocLitBareClientTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private Void beanFunctionCaller(Context context) {

        TestBean1 b1 = new TestBean1();
        b1.stringItem = "strung";
        TestBean1[] beans = new TestBean1[3];
        beans[0] = new TestBean1();
        beans[0].stringItem = "zerobean";
        beans[0].beanTwoNotRequiredItem = new TestBean2("bean2");
        beans[1] = null;
        beans[2] = new TestBean1();
        beans[2].stringItem = "twobean";
        beans[2].optionalIntArrayItem = new int[2];
        beans[2].optionalIntArrayItem[0] = 4;
        beans[2].optionalIntArrayItem[1] = 6;

        Object[] jsBeans = new Object[3];
        jsBeans[0] = testBean1ToJS(testUtilities, context, beans[0]);
        jsBeans[1] = null;
        jsBeans[2] = testBean1ToJS(testUtilities, context, beans[2]);

        Scriptable jsBean1 = testBean1ToJS(testUtilities, context, b1);
        Scriptable jsBeanArray = context.newArray(testUtilities.getRhinoScope(), jsBeans);

        LOG.info("About to call beanFunctionTest " + getAddress());
        Notifier notifier =
            testUtilities.rhinoCallConvert("beanFunctionTest", Notifier.class,
                                           testUtilities.javaToJS(getAddress()),
                                           jsBean1,
                                           jsBeanArray);
        boolean notified = notifier.waitForJavascript(1000 * 10);
        assertTrue(notified);
        Integer errorStatus = testUtilities.rhinoEvaluateConvert("globalErrorStatus", Integer.class);
        assertNull(errorStatus);
        String errorText = testUtilities.rhinoEvaluateConvert("globalErrorStatusText", String.class);
        assertNull(errorText);

        // this method returns void.
        Scriptable responseObject = (Scriptable)testUtilities.rhinoEvaluate("globalResponseObject");
        // there is no response, this thing returns 'void'
        assertNull(responseObject);
        SimpleDocLitBareImpl impl = (SimpleDocLitBareImpl)rawImplementor;
        TestBean1 b1returned = impl.getLastBean1();
        assertEquals(b1, b1returned);
        // commented out until
        //TestBean1[] beansReturned = impl.getLastBean1Array();
        //assertArrayEquals(beans, beansReturned);
        return null;
    }
 
Example 7
Source File: DocLitWrappedClientTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private Void beanFunctionCaller(Context context, boolean useWrapper) {
    TestBean1 b1 = new TestBean1();
    b1.stringItem = "strung";
    TestBean1[] beans = new TestBean1[3];
    beans[0] = new TestBean1();
    beans[0].beanTwoNotRequiredItem = new TestBean2("bean2");
    if (useWrapper) {
        beans[1] = null;
    } else {
        // without a wrapper, it can't be null, so put something in there.
        beans[1] = new TestBean1();
    }
    beans[2] = new TestBean1();
    beans[2].optionalIntArrayItem = new int[2];
    beans[2].optionalIntArrayItem[0] = 4;
    beans[2].optionalIntArrayItem[1] = 6;

    Object[] jsBeans = new Object[3];
    jsBeans[0] = testBean1ToJS(testUtilities, context, beans[0]);
    jsBeans[1] = testBean1ToJS(testUtilities, context, beans[1]);
    jsBeans[2] = testBean1ToJS(testUtilities, context, beans[2]);

    Scriptable jsBean1 = testBean1ToJS(testUtilities, context, b1);
    Scriptable jsBeanArray = context.newArray(testUtilities.getRhinoScope(), jsBeans);

    LOG.info("About to call test4 " + getAddress());
    Notifier notifier =
        testUtilities.rhinoCallConvert("test4", Notifier.class,
                                       testUtilities.javaToJS(getAddress()),
                                       testUtilities.javaToJS(Boolean.valueOf(useWrapper)),
                                       jsBean1,
                                       jsBeanArray);
    boolean notified = notifier.waitForJavascript(1000 * 10);
    assertTrue(notified);
    Integer errorStatus = testUtilities.rhinoEvaluateConvert("globalErrorStatus", Integer.class);
    assertNull(errorStatus);
    String errorText = testUtilities.rhinoEvaluateConvert("globalErrorStatusText", String.class);
    assertNull(errorText);

    // This method returns void, which translated into a Javascript object with no properties.
    // really. Void.
    Object responseObject = testUtilities.rhinoEvaluate("globalResponseObject");
    assertNotNull(responseObject);
    assertEquals(Context.getUndefinedValue(), responseObject);
    SimpleDocLitWrappedImpl impl = (SimpleDocLitWrappedImpl)rawImplementor;
    TestBean1 b1returned = impl.getLastBean1();
    assertEquals(b1, b1returned);
    TestBean1[] beansReturned = impl.getLastBean1Array();
    assertArrayEquals(beans, beansReturned);
    return null;
}