Java Code Examples for org.mozilla.javascript.ContextFactory.enterContext()

The following are Jave code examples for showing how to use enterContext() of the org.mozilla.javascript.ContextFactory class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: whackpad   File: JsTestsBase.java   Source Code and License Vote up 6 votes
public void runJsTests(File[] tests) throws IOException {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(this.optimizationLevel);
        Scriptable shared = cx.initStandardObjects();
        for (File f : tests) {
            int length = (int) f.length(); // don't worry about very long
                                           // files
            char[] buf = new char[length];
            new FileReader(f).read(buf, 0, length);
            String session = new String(buf);
            runJsTest(cx, shared, f.getName(), session);
        }
    } finally {
        Context.exit();
    }
}
 
Example 2
Project: whackpad   File: ContextFactoryTest.java   Source Code and License Vote up 6 votes
public void testCustomContextFactory() {
    ContextFactory factory = new MyFactory();
    Context cx = factory.enterContext();
    try {
        Scriptable globalScope = cx.initStandardObjects();
        // Test that FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME is enabled
        /* TODO(stevey): fix this functionality in parser
        Object result = cx.evaluateString(globalScope,
                "var obj = {};" +
                "function obj.foo() { return 'bar'; }" +
                "obj.foo();",
                "test source", 1, null);
        assertEquals("bar", result);
        */
    } catch (RhinoException e) {
        fail(e.toString());
    } finally {
        Context.exit();
    }
}
 
Example 3
Project: whackpad   File: DoctestsTest.java   Source Code and License Vote up 6 votes
@Test
public void runDoctest() throws Exception {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(optimizationLevel);
        Global global = new Global(cx);
        // global.runDoctest throws an exception on any failure
        int testsPassed = global.runDoctest(cx, global, source, name, 1);
        System.out.println(name + "(" + optimizationLevel + "): " +
                testsPassed + " passed.");
        assertTrue(testsPassed > 0);
    } catch (Exception ex) {
      System.out.println(name + "(" + optimizationLevel + "): FAILED due to "+ex);
      throw ex;
    } finally {
        Context.exit();
    }  
}
 
Example 4
Project: whackpad   File: CustomSetterAcceptNullScriptableTest.java   Source Code and License Vote up 6 votes
public void testSetNullForScriptableSetter() throws Exception {
	
	final String scriptCode = "foo.myProp = new Foo2();\n"
		+ "foo.myProp = null;";

	final ContextFactory factory = new ContextFactory();
	final Context cx = factory.enterContext();

	try {
        final ScriptableObject topScope = cx.initStandardObjects();
        final Foo foo = new Foo();

        // define custom setter method
        final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
        foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);

        topScope.put("foo", topScope, foo);
        
        ScriptableObject.defineClass(topScope, Foo2.class);
		
        cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
	}
	finally {
		Context.exit();
	}
}
 
Example 5
Project: code404   File: JsTestsBase.java   Source Code and License Vote up 6 votes
public void runJsTests(File[] tests) throws IOException {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(this.optimizationLevel);
        Scriptable shared = cx.initStandardObjects();
        for (File f : tests) {
            int length = (int) f.length(); // don't worry about very long
                                           // files
            char[] buf = new char[length];
            new FileReader(f).read(buf, 0, length);
            String session = new String(buf);
            runJsTest(cx, shared, f.getName(), session);
        }
    } finally {
        Context.exit();
    }
}
 
Example 6
Project: code404   File: ContextFactoryTest.java   Source Code and License Vote up 6 votes
public void testCustomContextFactory() {
    ContextFactory factory = new MyFactory();
    Context cx = factory.enterContext();
    try {
        Scriptable globalScope = cx.initStandardObjects();
        // Test that FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME is enabled
        /* TODO(stevey): fix this functionality in parser
        Object result = cx.evaluateString(globalScope,
                "var obj = {};" +
                "function obj.foo() { return 'bar'; }" +
                "obj.foo();",
                "test source", 1, null);
        assertEquals("bar", result);
        */
    } catch (RhinoException e) {
        fail(e.toString());
    } finally {
        Context.exit();
    }
}
 
Example 7
Project: code404   File: DoctestsTest.java   Source Code and License Vote up 6 votes
@Test
public void runDoctest() throws Exception {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(optimizationLevel);
        Global global = new Global(cx);
        // global.runDoctest throws an exception on any failure
        int testsPassed = global.runDoctest(cx, global, source, name, 1);
        System.out.println(name + "(" + optimizationLevel + "): " +
                testsPassed + " passed.");
        assertTrue(testsPassed > 0);
    } catch (Exception ex) {
      System.out.println(name + "(" + optimizationLevel + "): FAILED due to "+ex);
      throw ex;
    } finally {
        Context.exit();
    }
}
 
Example 8
Project: code404   File: CustomSetterAcceptNullScriptableTest.java   Source Code and License Vote up 6 votes
public void testSetNullForScriptableSetter() throws Exception {

		final String scriptCode = "foo.myProp = new Foo2();\n"
			+ "foo.myProp = null;";

		final ContextFactory factory = new ContextFactory();
		final Context cx = factory.enterContext();

		try {
	        final ScriptableObject topScope = cx.initStandardObjects();
	        final Foo foo = new Foo();

	        // define custom setter method
	        final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
	        foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);

	        topScope.put("foo", topScope, foo);

	        ScriptableObject.defineClass(topScope, Foo2.class);

	        cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
		}
		finally {
			Context.exit();
		}
	}
 
Example 9
Project: rhino-jscover   File: JsTestsBase.java   Source Code and License Vote up 6 votes
public void runJsTests(File[] tests) throws IOException {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(this.optimizationLevel);
        Scriptable shared = cx.initStandardObjects();
        for (File f : tests) {
            int length = (int) f.length(); // don't worry about very long
                                           // files
            char[] buf = new char[length];
            new FileReader(f).read(buf, 0, length);
            String session = new String(buf);
            runJsTest(cx, shared, f.getName(), session);
        }
    } finally {
        Context.exit();
    }
}
 
Example 10
Project: rhino-jscover   File: ContextFactoryTest.java   Source Code and License Vote up 6 votes
public void testCustomContextFactory() {
    ContextFactory factory = new MyFactory();
    Context cx = factory.enterContext();
    try {
        Scriptable globalScope = cx.initStandardObjects();
        // Test that FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME is enabled
        /* TODO(stevey): fix this functionality in parser
        Object result = cx.evaluateString(globalScope,
                "var obj = {};" +
                "function obj.foo() { return 'bar'; }" +
                "obj.foo();",
                "test source", 1, null);
        assertEquals("bar", result);
        */
    } catch (RhinoException e) {
        fail(e.toString());
    } finally {
        Context.exit();
    }
}
 
Example 11
Project: rhino-jscover   File: DoctestsTest.java   Source Code and License Vote up 6 votes
@Test
public void runDoctest() throws Exception {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(optimizationLevel);
        Global global = new Global(cx);
        // global.runDoctest throws an exception on any failure
        int testsPassed = global.runDoctest(cx, global, source, name, 1);
        System.out.println(name + "(" + optimizationLevel + "): " +
                testsPassed + " passed.");
        assertTrue(testsPassed > 0);
    } catch (Exception ex) {
      System.out.println(name + "(" + optimizationLevel + "): FAILED due to "+ex);
      throw ex;
    } finally {
        Context.exit();
    }
}
 
Example 12
Project: rhino-jscover   File: CustomSetterAcceptNullScriptableTest.java   Source Code and License Vote up 6 votes
public void testSetNullForScriptableSetter() throws Exception {

		final String scriptCode = "foo.myProp = new Foo2();\n"
			+ "foo.myProp = null;";

		final ContextFactory factory = new ContextFactory();
		final Context cx = factory.enterContext();

		try {
	        final ScriptableObject topScope = cx.initStandardObjects();
	        final Foo foo = new Foo();

	        // define custom setter method
	        final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
	        foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);

	        topScope.put("foo", topScope, foo);

	        ScriptableObject.defineClass(topScope, Foo2.class);

	        cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
		}
		finally {
			Context.exit();
		}
	}
 
Example 13
Project: astor   File: JsTestsBase.java   Source Code and License Vote up 6 votes
public void runJsTests(File[] tests) throws IOException {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(this.optimizationLevel);
        Scriptable shared = cx.initStandardObjects();
        for (File f : tests) {
            int length = (int) f.length(); // don't worry about very long
                                           // files
            char[] buf = new char[length];
            new FileReader(f).read(buf, 0, length);
            String session = new String(buf);
            runJsTest(cx, shared, f.getName(), session);
        }
    } finally {
        Context.exit();
    }
}
 
Example 14
Project: astor   File: ContextFactoryTest.java   Source Code and License Vote up 6 votes
public void testCustomContextFactory() {
    ContextFactory factory = new MyFactory();
    Context cx = factory.enterContext();
    try {
        Scriptable globalScope = cx.initStandardObjects();
        // Test that FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME is enabled
        /* TODO(stevey): fix this functionality in parser
        Object result = cx.evaluateString(globalScope,
                "var obj = {};" +
                "function obj.foo() { return 'bar'; }" +
                "obj.foo();",
                "test source", 1, null);
        assertEquals("bar", result);
        */
    } catch (RhinoException e) {
        fail(e.toString());
    } finally {
        Context.exit();
    }
}
 
Example 15
Project: astor   File: DoctestsTest.java   Source Code and License Vote up 6 votes
@Test
public void runDoctest() throws Exception {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(optimizationLevel);
        Global global = new Global(cx);
        // global.runDoctest throws an exception on any failure
        int testsPassed = global.runDoctest(cx, global, source, name, 1);
        System.out.println(name + "(" + optimizationLevel + "): " +
                testsPassed + " passed.");
        assertTrue(testsPassed > 0);
    } catch (Exception ex) {
      System.out.println(name + "(" + optimizationLevel + "): FAILED due to "+ex);
      throw ex;
    } finally {
        Context.exit();
    }
}
 
Example 16
Project: astor   File: CustomSetterAcceptNullScriptableTest.java   Source Code and License Vote up 6 votes
public void testSetNullForScriptableSetter() throws Exception {

		final String scriptCode = "foo.myProp = new Foo2();\n"
			+ "foo.myProp = null;";

		final ContextFactory factory = new ContextFactory();
		final Context cx = factory.enterContext();

		try {
	        final ScriptableObject topScope = cx.initStandardObjects();
	        final Foo foo = new Foo();

	        // define custom setter method
	        final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
	        foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);

	        topScope.put("foo", topScope, foo);

	        ScriptableObject.defineClass(topScope, Foo2.class);

	        cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
		}
		finally {
			Context.exit();
		}
	}
 
Example 17
Project: AnalyzerBeans   File: JavaScriptAdvancedTransformer.java   Source Code and License Vote up 6 votes
@Initialize
public void init() {
    _contextFactory = new ContextFactory();

    Context context = _contextFactory.enterContext();
    try {
        _script = context.compileString(sourceCode, this.getClass().getSimpleName(), 1, null);
        _sharedScope = context.initStandardObjects();

        JavaScriptUtils.addToScope(_sharedScope, logger, "logger", "log");
        JavaScriptUtils.addToScope(_sharedScope, System.out, "out");

        _script.exec(context, _sharedScope);
        _transformerObj = (NativeObject) _sharedScope.get("transformerObj");
        if (_transformerObj == null) {
            throw new IllegalStateException("Required JS object 'transformerObj' not found!");
        }

        _initializeFunction = (Function) _transformerObj.get("initialize");
        _transformFunction = (Function) _transformerObj.get("transform");
        _closeFunction = (Function) _transformerObj.get("close");

        _initializeFunction.call(context, _sharedScope, _sharedScope, new Object[0]);
    } finally {
        Context.exit();
    }
}
 
Example 18
Project: AnalyzerBeans   File: JavaScriptTransformer.java   Source Code and License Vote up 6 votes
@Initialize
public void init() {
	_contextFactory = new ContextFactory();
	Context context = _contextFactory.enterContext();

	try {
		_script = context.compileString(sourceCode, this.getClass()
				.getSimpleName(), 1, null);
		_sharedScope = context.initStandardObjects();

		JavaScriptUtils.addToScope(_sharedScope, logger, "logger", "log");
		JavaScriptUtils.addToScope(_sharedScope, System.out, "out");
	} finally {
		Context.exit();
	}
}
 
Example 19
Project: Rhino-Prov-Mod   File: JsTestsBase.java   Source Code and License Vote up 6 votes
public void runJsTests(File[] tests) throws IOException {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(this.optimizationLevel);
        Scriptable shared = cx.initStandardObjects();
        for (File f : tests) {
            int length = (int) f.length(); // don't worry about very long
                                           // files
            char[] buf = new char[length];
            new FileReader(f).read(buf, 0, length);
            String session = new String(buf);
            runJsTest(cx, shared, f.getName(), session);
        }
    } finally {
        Context.exit();
    }
}
 
Example 20
Project: Rhino-Prov-Mod   File: ContextFactoryTest.java   Source Code and License Vote up 6 votes
public void testCustomContextFactory() {
    ContextFactory factory = new MyFactory();
    Context cx = factory.enterContext();
    try {
        Scriptable globalScope = cx.initStandardObjects();
        // Test that FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME is enabled
        /* TODO(stevey): fix this functionality in parser
        Object result = cx.evaluateString(globalScope,
                "var obj = {};" +
                "function obj.foo() { return 'bar'; }" +
                "obj.foo();",
                "test source", 1, null);
        assertEquals("bar", result);
        */
    } catch (RhinoException e) {
        fail(e.toString());
    } finally {
        Context.exit();
    }
}
 
Example 21
Project: Rhino-Prov-Mod   File: DoctestsTest.java   Source Code and License Vote up 6 votes
@Test
public void runDoctest() throws Exception {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(optimizationLevel);
        Global global = new Global(cx);
        // global.runDoctest throws an exception on any failure
        int testsPassed = global.runDoctest(cx, global, source, name, 1);
        System.out.println(name + "(" + optimizationLevel + "): " +
                testsPassed + " passed.");
        assertTrue(testsPassed > 0);
    } catch (Exception ex) {
      System.out.println(name + "(" + optimizationLevel + "): FAILED due to "+ex);
      throw ex;
    } finally {
        Context.exit();
    }
}
 
Example 22
Project: Rhino-Prov-Mod   File: CustomSetterAcceptNullScriptableTest.java   Source Code and License Vote up 6 votes
public void testSetNullForScriptableSetter() throws Exception {

		final String scriptCode = "foo.myProp = new Foo2();\n"
			+ "foo.myProp = null;";

		final ContextFactory factory = new ContextFactory();
		final Context cx = factory.enterContext();

		try {
	        final ScriptableObject topScope = cx.initStandardObjects();
	        final Foo foo = new Foo();

	        // define custom setter method
	        final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
	        foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);

	        topScope.put("foo", topScope, foo);

	        ScriptableObject.defineClass(topScope, Foo2.class);

	        cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
		}
		finally {
			Context.exit();
		}
	}
 
Example 23
Project: whackpad   File: Utils.java   Source Code and License Vote up 5 votes
/**
 * Runs the provided action at the given optimization level
 */
public static void runWithOptimizationLevel(final ContextFactory contextFactory, final ContextAction action, final int optimizationLevel)
{
   	final Context cx = contextFactory.enterContext();
   	try
   	{
   		cx.setOptimizationLevel(optimizationLevel);
   		action.run(cx);
   	}
   	finally
   	{
   		Context.exit();
   	}
}
 
Example 24
Project: rhino-android   File: Utils.java   Source Code and License Vote up 5 votes
/**
 * Runs the provided action at the given optimization level
 */
public static void runWithOptimizationLevel(final ContextFactory contextFactory, final ContextAction action, final int optimizationLevel)
{
   	final Context cx = contextFactory.enterContext();
   	try
   	{
   		cx.setOptimizationLevel(optimizationLevel);
   		action.run(cx);
   	}
   	finally
   	{
   		Context.exit();
   	}
}
 
Example 25
Project: JsSandbox   File: JsSandboxEnvironment.java   Source Code and License Vote up 5 votes
public Context getContext()
{
    if (cx == null) {
        ContextFactory contextFactory = new ContextFactory();
        cx = contextFactory.enterContext();
        initFuncs();
    }
    return cx;
}
 
Example 26
Project: nm9   File: RhinoScriptHost.java   Source Code and License Vote up 5 votes
/**
 * Create a script host
 * @param optimizationLevel Rhino optimization level (Valid range is -1 between 9, must be -1 on Android)
 */
public RhinoScriptHost(int optimizationLevel) {
	cxf = new ContextFactory();
	cx = cxf.enterContext();
	cx.setOptimizationLevel(optimizationLevel);
	scope = cx.initStandardObjects();
}
 
Example 27
Project: code404   File: Utils.java   Source Code and License Vote up 5 votes
/**
 * Runs the provided action at the given optimization level
 */
public static void runWithOptimizationLevel(final ContextFactory contextFactory, final ContextAction action, final int optimizationLevel)
{
   	final Context cx = contextFactory.enterContext();
   	try
   	{
   		cx.setOptimizationLevel(optimizationLevel);
   		action.run(cx);
   	}
   	finally
   	{
   		Context.exit();
   	}
}
 
Example 28
Project: DataCleaner   File: JavaScriptFilter.java   Source Code and License Vote up 5 votes
@Initialize
public void init() {
    _contextFactory = new ContextFactory();
    final Context context = _contextFactory.enterContext();

    try {
        _script = context.compileString(sourceCode, this.getClass().getSimpleName(), 1, null);
        _sharedScope = context.initStandardObjects();

        JavaScriptUtils.addToScope(_sharedScope, new JavaScriptLogger(), "logger", "log");
        JavaScriptUtils.addToScope(_sharedScope, System.out, "out");
    } finally {
        Context.exit();
    }
}
 
Example 29
Project: Rhino-Prov-Mod   File: Utils.java   Source Code and License Vote up 5 votes
/**
 * Runs the provided action at the given optimization level
 */
public static void runWithOptimizationLevel(final ContextFactory contextFactory, final ContextAction action, final int optimizationLevel)
{
   	final Context cx = contextFactory.enterContext();
   	try
   	{
   		cx.setOptimizationLevel(optimizationLevel);
   		action.run(cx);
   	}
   	finally
   	{
   		Context.exit();
   	}
}
 
Example 30
Project: DataCleaner   File: JavaScriptTransformer.java   Source Code and License Vote up 5 votes
@Initialize
public void init() {
    _contextFactory = new ContextFactory();
    final Context context = _contextFactory.enterContext();

    try {
        _script = context.compileString(sourceCode, this.getClass().getSimpleName(), 1, null);
        _sharedScope = context.initStandardObjects();

        JavaScriptUtils.addToScope(_sharedScope, new JavaScriptLogger(), "logger", "log");
        JavaScriptUtils.addToScope(_sharedScope, System.out, "out");
    } finally {
        Context.exit();
    }
}
 
Example 31
Project: rhino-jscover   File: Utils.java   Source Code and License Vote up 5 votes
/**
 * Runs the provided action at the given optimization level
 */
public static void runWithOptimizationLevel(final ContextFactory contextFactory, final ContextAction action, final int optimizationLevel)
{
   	final Context cx = contextFactory.enterContext();
   	try
   	{
   		cx.setOptimizationLevel(optimizationLevel);
   		action.run(cx);
   	}
   	finally
   	{
   		Context.exit();
   	}
}
 
Example 32
Project: astor   File: Utils.java   Source Code and License Vote up 5 votes
/**
 * Runs the provided action at the given optimization level
 */
public static void runWithOptimizationLevel(final ContextFactory contextFactory, final ContextAction action, final int optimizationLevel)
{
   	final Context cx = contextFactory.enterContext();
   	try
   	{
   		cx.setOptimizationLevel(optimizationLevel);
   		action.run(cx);
   	}
   	finally
   	{
   		Context.exit();
   	}
}
 
Example 33
Project: AnalyzerBeans   File: JavaScriptFilter.java   Source Code and License Vote up 5 votes
@Initialize
public void init() {
    _contextFactory = new ContextFactory();
    Context context = _contextFactory.enterContext();

    try {
        _script = context.compileString(sourceCode, this.getClass().getSimpleName(), 1, null);
        _sharedScope = context.initStandardObjects();

        JavaScriptUtils.addToScope(_sharedScope, logger, "logger", "log");
        JavaScriptUtils.addToScope(_sharedScope, System.out, "out");
    } finally {
        Context.exit();
    }
}
 
Example 34
Project: taskexecutor   File: DebuggerTest.java   Source Code and License Vote up 5 votes
@Test
     public void debug() {

//       ContextFactory contextFactory = getDebugContextFactory();
        ContextFactory contextFactory = getNormalContextFactory();
       Scriptable scope; // this is THE OBJECT
       Context cx;
       


        cx = contextFactory.enterContext();
        try {
            scope = cx.initStandardObjects();
        } finally {
            Context.exit();
        }

       displayDebugger2(contextFactory, scope);

        cx = contextFactory.enterContext();
        try {
            Object result = cx.evaluateString(scope, "2 + 4 + 5", "<cmd>", 1, null);
             result = cx.evaluateString(scope, "2 + 55 + 5", "<cmd2>", 1, null);
            result = cx.evaluateString(scope, "1/3", "<cmd3>", 1, null);
            result = cx.evaluateString(scope, "32323.333+3232", "<cmd4>", 1, null);
            result = cx.evaluateString(scope, "'cocoloco'", "<cmd5>", 1, null);
            System.out.println(Context.toString(result));
        } finally {
            Context.exit();
        }
     }
 
Example 35
Project: cyc   File: Game.java   Source Code and License Vote up 4 votes
private void readScripts(List<User> userList) {

		ContextFactory contextFactory = ContextFactory.getGlobal();
		Context context = contextFactory.enterContext();
		try {

			for (User user : userList) {
				Company company = new Company(this, user.getUsername(), grocer);
				ThreadLocal.setCompany(company);

				ScriptableObject prototype = context.initStandardObjects();
				prototype.setParentScope(null);
				Scriptable scope = context.newObject(prototype);
				scope.setPrototype(prototype);

				Object jsCompany = new SandboxNativeJavaObject(scope, company, Company.class);
				prototype.put("company", scope, jsCompany);
				Object jsSystemout = new SandboxNativeJavaObject(scope,
						new DebugAdapter(this, gameRun.getResult(), company.getName()), DebugAdapter.class);
				prototype.put("out", scope, jsSystemout);
				prototype.put("console", scope, jsSystemout);

				try {
					long time = System.nanoTime();
					context.evaluateString(scope, user.getMainJavaScript(), company.getName(), 1, null);
					getResult().getCreateNotExists(company.getName()).addRunTime("init", System.nanoTime() - time);
					getResult().getCreateNotExists(company.getName()).setCode(user.getMainJavaScript());
				} catch (RhinoException e) {
					if (e.getCause() instanceof GameException) {
						log.info("Failed to initialize the JavaScript, but found a GameException", e);
					} else {
						String formattedStackTrace = ExceptionConverter.convertToString(e);
						gameRun.getResult().addError(formattedStackTrace);
						log.error("Failed to initialize the JavaScript. Player " + company.getName() + " bankrupt", e);
						company.setBankruptFromError(formattedStackTrace);
					}
				}

				companies.add(company);
			}
			ThreadLocal.resetCompany();
		} finally {
			Context.exit();
		}
	}