Java Code Examples for org.mozilla.javascript.ScriptableObject#putProperty()
The following examples show how to use
org.mozilla.javascript.ScriptableObject#putProperty() .
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: JsRuntimeRepl.java From stetho with MIT License | 6 votes |
@Override public @Nullable Object evaluate(@NonNull String expression) throws Throwable { Object result; final Context jsContext = enterJsContext(); try { result = jsContext.evaluateString(mJsScope, expression, "chrome", 1, null); // Google chrome automatically saves the last expression to `$_`, we do the same Object jsValue = Context.javaToJS(result, mJsScope); ScriptableObject.putProperty(mJsScope, "$_", jsValue); } finally { Context.exit(); } return Context.jsToJava(result, Object.class); }
Example 2
Source File: EcmaScriptDataModel.java From JVoiceXML with GNU Lesser General Public License v2.1 | 6 votes |
private int createArray(final String arrayName, final int dimension, final Scope scope, final Scriptable start) { if (start == null) { return ERROR_SCOPE_NOT_FOUND; } final Scriptable targetScope = getAndCreateScope(start, arrayName); final String name; int dotPos = arrayName.lastIndexOf('.'); if (dotPos >= 0) { name = arrayName.substring(dotPos + 1); } else { name = arrayName; } if (ScriptableObject.hasProperty(targetScope, name)) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("'" + arrayName + "' already defined"); } return ERROR_VARIABLE_ALREADY_DEFINED; } final NativeArray array = new NativeArray(dimension); ScriptableObject.putProperty(targetScope, name, array); if (LOGGER.isDebugEnabled()) { LOGGER.debug("created '" + arrayName + "' in scope '" + getScope(targetScope) + "' as an array of " + dimension); } // Fill the array for (int i = 0; i < dimension; i++) { final Context context = getContext(); final Scriptable scriptable = context.newObject(topmostScope); ScriptableObject.putProperty(array, i, scriptable); } return NO_ERROR; }
Example 3
Source File: JsonExtensionNotificationDataConverter.java From JVoiceXML with GNU Lesser General Public License v2.1 | 5 votes |
/** * Packs the given SSML document into a JSON structure. * * @param speakable * the speakable with the SSML document * @return JSON formatted string */ private String toJson(final SpeakableText speakable) { final Context context = Context.enter(); context.setLanguageVersion(Context.VERSION_DEFAULT); final ScriptableObject scope = context.initStandardObjects(); final String ssml = speakable.getSpeakableText(); final Scriptable vxml = context.newObject(scope); ScriptableObject.putProperty(vxml, "ssml", ssml); return toJSON(vxml); }
Example 4
Source File: JsRuntimeReplFactoryBuilder.java From stetho with MIT License | 5 votes |
private void importFunctions(@NonNull ScriptableObject scope) throws StethoJsException { // Define the functions for (Map.Entry<String, Function> entrySet : mFunctions.entrySet()) { String functionName = entrySet.getKey(); Function function = entrySet.getValue(); try { ScriptableObject.putProperty(scope, functionName, function); } catch (Exception e) { throw new StethoJsException(e, "Failed to setup function: %s", functionName); } } }
Example 5
Source File: EcmaScriptDataModel.java From JVoiceXML with GNU Lesser General Public License v2.1 | 5 votes |
private int updateVariable(final String variableName, final Object newValue, final Scope scope, final Scriptable start) { if (start == null) { return ERROR_SCOPE_NOT_FOUND; } final Scriptable subscope = getAndCreateScope(start, variableName); final String property; int dotPos = variableName.lastIndexOf('.'); if (dotPos >= 0) { property = variableName.substring(dotPos + 1); } else { property = variableName; } if (!ScriptableObject.hasProperty(subscope, property)) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("'" + variableName + "' not found"); } return ERROR_VARIABLE_NOT_FOUND; } final Object jsValue = Context.javaToJS(newValue, subscope); ScriptableObject.putProperty(subscope, property, jsValue); if (LOGGER.isDebugEnabled()) { final String json = toString(jsValue); if (scope == null) { LOGGER.debug("set '" + variableName + "' in scope '" + getScope(subscope) + "' to '" + json + "'"); } else { LOGGER.debug("set '" + variableName + "' in scope '" + getScope(subscope) + "' to '" + json + "'"); } } return 0; }
Example 6
Source File: Rhino.java From helloiot with GNU General Public License v3.0 | 5 votes |
@Override public void putScopeMap(Map<String, Object> params) throws ScriptExecException { try { Context cx = Context.enter(); cx.setOptimizationLevel(-1); // always interpretive mode for (Map.Entry<String, Object> entry : params.entrySet()) { ScriptableObject.putProperty(scope, entry.getKey(), Context.javaToJS(entry.getValue(), scope)); } } finally { Context.exit(); } }
Example 7
Source File: ComplianceTest.java From rhino-android with Apache License 2.0 | 5 votes |
@org.junit.Test public void testRequire() throws Throwable { final Context cx = RhinoAndroidHelper.prepareContext(); try { cx.setOptimizationLevel(-1); final Scriptable scope = cx.initStandardObjects(); ScriptableObject.putProperty(scope, "print", new Print(scope)); createRequire(testDir, cx, scope).requireMain(cx, "program"); } finally { Context.exit(); } }
Example 8
Source File: RequireJarTest.java From astor with GNU General Public License v2.0 | 5 votes |
public void testNonSandboxed() throws Exception { final Context cx = createContext(); final Scriptable scope = cx.initStandardObjects(); final Require require = getSandboxedRequire(cx, scope, false); final String jsFile = getClass().getResource("testNonSandboxed.js").toExternalForm(); ScriptableObject.putProperty(scope, "moduleUri", jsFile); require.requireMain(cx, "testNonSandboxed"); }
Example 9
Source File: EcmaScriptDataModel.java From JVoiceXML with GNU Lesser General Public License v2.1 | 5 votes |
/** * Actually create the variable in the specified scope. * * @param variableName * the name of the variable * @param value * the initial value of the variable * @param scope * logical scope where to create the variable * @param start * scriptable scope where to create the variable * @return {@code 0} if successful */ private int createVariable(final String variableName, final Object value, final Scope scope, final Scriptable start) { if (start == null) { return ERROR_SCOPE_NOT_FOUND; } final Scriptable subscope = getAndCreateScope(start, variableName); if (subscope == null) { LOGGER.warn("unable to create scope for '" + variableName + "'." + " Please check object creation in var tag or in your" + " grammar file."); return ERROR_SCOPE_NOT_FOUND; } final String name; int dotPos = variableName.lastIndexOf('.'); if (dotPos >= 0) { name = variableName.substring(dotPos + 1); } else { name = variableName; } if (ScriptableObject.hasProperty(subscope, name)) { LOGGER.warn("'" + variableName + "' already defined"); return ERROR_VARIABLE_ALREADY_DEFINED; } final Object wrappedValue = Context.javaToJS(value, start); ScriptableObject.putProperty(subscope, name, wrappedValue); if (LOGGER.isDebugEnabled()) { final String json = toString(wrappedValue); if (scope == null) { LOGGER.debug("created '" + variableName + "' in scope '" + getScope(subscope) + "' with '" + json + "'"); } else { LOGGER.debug("created '" + variableName + "' in scope '" + getScope(subscope) + "' with '" + json + "'"); } } return NO_ERROR; }
Example 10
Source File: A6Util.java From Huochexing12306 with Apache License 2.0 | 5 votes |
/** * 执行JS * * @param js js代码 * @param functionName js方法名称 * @param functionParams js方法参数 * @return */ public static String runScript(Context context, String js, String functionName, Object[] functionParams) { org.mozilla.javascript.Context rhino = org.mozilla.javascript.Context.enter(); rhino.setOptimizationLevel(-1); try { Scriptable scope = rhino.initStandardObjects(); ScriptableObject.putProperty(scope, "javaContext", org.mozilla.javascript.Context.javaToJS(context, scope)); ScriptableObject.putProperty(scope, "javaLoader", org.mozilla.javascript.Context.javaToJS(context.getClass().getClassLoader(), scope)); rhino.evaluateString(scope, js, context.getClass().getSimpleName(), 1, null); Function function = (Function) scope.get(functionName, scope); Object result = function.call(rhino, scope, scope, functionParams); if (result instanceof String) { return (String) result; } else if (result instanceof NativeJavaObject) { return (String) ((NativeJavaObject) result).getDefaultValue(String.class); } else if (result instanceof NativeObject) { return (String) ((NativeObject) result).getDefaultValue(String.class); } return result.toString();//(String) function.call(rhino, scope, scope, functionParams); } finally { org.mozilla.javascript.Context.exit(); } }
Example 11
Source File: RhinoShellScope.java From elasticshell with Apache License 2.0 | 5 votes |
@Override public <T> void registerJavaObject(String name, T javaObject) { //pretty ugly but works, good enough for now ;) RhinoCustomNativeJavaObject rhinoCustomNativeJavaObject; if (javaObject instanceof RhinoCustomNativeJavaObject) { rhinoCustomNativeJavaObject = (RhinoCustomNativeJavaObject)javaObject; } else { rhinoCustomNativeJavaObject = new RhinoCustomNativeJavaObject(get(), javaObject, javaObject.getClass()); } ScriptableObject.putProperty(get(), name, rhinoCustomNativeJavaObject); }
Example 12
Source File: JSAcceptFacet.java From emissary with Apache License 2.0 | 5 votes |
/** * Make a scope in the current context with our required global variables in it * * @param ctx the current Rhino context * @return the new scope */ protected Scriptable makeAcceptFacetScope(final Context ctx) { final Scriptable scope = ctx.initStandardObjects(); final Object jsLogger = Context.javaToJS(this.scriptLogger, scope); ScriptableObject.putProperty(scope, "logger", jsLogger); final Object jsOut = Context.javaToJS(System.out, scope); ScriptableObject.putProperty(scope, "out", jsOut); return scope; }
Example 13
Source File: JSEngine.java From movienow with GNU General Public License v3.0 | 5 votes |
public synchronized String evalJS(String jsStr, String input) { String js = "var input = '" + input + "';" + jsStr.replace(";;",";"); org.mozilla.javascript.Context rhino = org.mozilla.javascript.Context.enter(); rhino.setOptimizationLevel(-1); try { Scriptable scope = rhino.initStandardObjects(); ScriptableObject.putProperty(scope, "javaContext", org.mozilla.javascript.Context.javaToJS(this, scope));//配置属性 javaContext:当前类JSEngine的上下文 ScriptableObject.putProperty(scope, "javaLoader", org.mozilla.javascript.Context.javaToJS(clazz.getClassLoader(), scope));//配置属性 javaLoader:当前类的JSEngine的类加载器 return (String) rhino.evaluateString(scope, js, clazz.getSimpleName(), 1, null); } finally { org.mozilla.javascript.Context.exit(); } }
Example 14
Source File: NativeDateTimeSpanTest.java From birt with Eclipse Public License 1.0 | 4 votes |
@Test public void testSubTime( ) { Calendar dateCal = getCalendarInstance( ); dateCal.set( 2008, 8, 13, 20, 1, 44 ); Date date1 = dateCal.getTime( ); Object jsNumber1 = Context.javaToJS( date1, scope ); ScriptableObject.putProperty( scope, "date1", jsNumber1 ); String script1 = "DateTimeSpan.subTime(date1,-1,-1,-1);"; Object value1 = eval( script1 ); Calendar cal = getCalendarInstance( ); cal.setTime( (Date) value1 ); assertEquals( 2008, cal.get( Calendar.YEAR ) ); assertEquals( 8, cal.get( Calendar.MONTH ) ); assertEquals( 13, cal.get( Calendar.DATE ) ); assertEquals( 21, cal.get( Calendar.HOUR_OF_DAY ) ); assertEquals( 2, cal.get( Calendar.MINUTE ) ); assertEquals( 45, cal.get( Calendar.SECOND ) ); System.out.println( "1" ); /* * More than 24 hours */ String script2 = "DateTimeSpan.subTime(date1,-25,-1,-1);"; Object value2 = eval( script2 ); cal = getCalendarInstance( ); cal.setTime( (Date) value2 ); assertEquals( 2008, cal.get( Calendar.YEAR ) ); assertEquals( 8, cal.get( Calendar.MONTH ) ); assertEquals( 14, cal.get( Calendar.DATE ) ); assertEquals( 21, cal.get( Calendar.HOUR_OF_DAY ) ); assertEquals( 2, cal.get( Calendar.MINUTE ) ); assertEquals( 45, cal.get( Calendar.SECOND ) ); System.out.println( "2" ); /* * Add negtive arguments. */ String script5 = "DateTimeSpan.subTime(date1,1,1,1);"; Object value5 = eval( script5 ); cal = getCalendarInstance( ); cal.setTime( (Date) value5 ); assertEquals( 2008, cal.get( Calendar.YEAR ) ); assertEquals( 8, cal.get( Calendar.MONTH ) ); assertEquals( 13, cal.get( Calendar.DATE ) ); assertEquals( 19, cal.get( Calendar.HOUR_OF_DAY ) ); assertEquals( 0, cal.get( Calendar.MINUTE ) ); assertEquals( 43, cal.get( Calendar.SECOND ) ); System.out.println( "5" ); String script6 = "DateTimeSpan.subTime(date1,1,1,61);"; Object value6 = eval( script6 ); cal = getCalendarInstance( ); cal.setTime( (Date) value6 ); assertEquals( 2008, cal.get( Calendar.YEAR ) ); assertEquals( 8, cal.get( Calendar.MONTH ) ); assertEquals( 13, cal.get( Calendar.DATE ) ); assertEquals( 18, cal.get( Calendar.HOUR_OF_DAY ) ); assertEquals( 59, cal.get( Calendar.MINUTE ) ); assertEquals( 43, cal.get( Calendar.SECOND ) ); System.out.println( "6" ); }
Example 15
Source File: EcmaScriptDataModel.java From JVoiceXML with GNU Lesser General Public License v2.1 | 4 votes |
private int updateArray(final String variableName, final int position, final Object newValue, final Scope scope, final Scriptable start) { if (start == null) { return ERROR_SCOPE_NOT_FOUND; } final Scriptable subscope = getAndCreateScope(start, variableName); final String property; int dotPos = variableName.lastIndexOf('.'); if (dotPos >= 0) { property = variableName.substring(dotPos + 1); } else { property = variableName; } if (!ScriptableObject.hasProperty(subscope, property)) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("'" + variableName + "' not found"); } return ERROR_VARIABLE_NOT_FOUND; } final Object jsValue = Context.javaToJS(newValue, subscope); final NativeArray array = (NativeArray) ScriptableObject.getProperty( subscope, property); if (!ScriptableObject.hasProperty(array, position)) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("'" + variableName + "' has no position " + position); } return ERROR_VARIABLE_NOT_FOUND; } ScriptableObject.putProperty(array, position, jsValue); if (LOGGER.isDebugEnabled()) { final String json = toString(jsValue); if (scope == null) { LOGGER.debug("set '" + variableName + "[" + position + "]' to '" + json + "'"); } else { LOGGER.debug("set '" + variableName + "[" + position + "]' to '" + json + "' in scope '" + scope + "'"); } } return 0; }
Example 16
Source File: RhinoShellScope.java From elasticshell with Apache License 2.0 | 4 votes |
@Override public void registerJavaClass(Class<?> clazz) { ScriptableObject.putProperty(get(), clazz.getSimpleName(), new RhinoCustomNativeJavaClass(get(), clazz)); }
Example 17
Source File: NativeDateTimeSpanTest.java From birt with Eclipse Public License 1.0 | 4 votes |
@Test public void testSubDate( ) { /* * Add a single year */ String script1 = "var startDate=\"2/3/08 8:01 PM\" ;var years=1;var months=0;var days=0;" + "DateTimeSpan.subDate(startDate,years,months,days);"; Object value1 = eval( script1 ); Calendar cal = getCalendarInstance( ); cal.setTime( (Date) value1 ); assertEquals( 2007, cal.get( Calendar.YEAR ) ); assertEquals( 1, cal.get( Calendar.MONTH ) ); assertEquals( 3, cal.get( Calendar.DATE ) ); System.out.println( "1" ); /* * Use Date instance to test this method. */ Calendar dateCal = getCalendarInstance( ); dateCal.set( 2008, 8, 13, 20, 1, 44 ); Date date1 = dateCal.getTime( ); Object jsNumber1 = Context.javaToJS( date1, scope ); ScriptableObject.putProperty( scope, "date1", jsNumber1 ); String script2 = "DateTimeSpan.subDate(date1,1,0,0);"; Object value2 = eval( script2 ); cal = getCalendarInstance( ); cal.setTime( (Date) value2 ); assertEquals( 2007, cal.get( Calendar.YEAR ) ); assertEquals( 8, cal.get( Calendar.MONTH ) ); assertEquals( 13, cal.get( Calendar.DATE ) ); System.out.println( "2" ); /* * Add one year to the startDate, and since 2008 is leap year, so the * result date is 3/1/09. */ String script3 = "var startDate=\"2/29/08 8:01 PM\" ;var years=1;var months=0;var days=0;" + "DateTimeSpan.subDate(startDate,years,months,days);"; Object value3 = eval( script3 ); cal = getCalendarInstance( ); cal.setTime( (Date) value3 ); assertEquals( 2007, cal.get( Calendar.YEAR ) ); assertEquals( 1, cal.get( Calendar.MONTH ) ); assertEquals( 28, cal.get( Calendar.DATE ) ); System.out.println( "3" ); /* * Adding one month to Jan.31 would produce the invalid date Feb.31. The * method adjusts the date to be valid, in this case, if the year is not * a leap year, then Feb. has 28 days and the resulting date would be * Mar.3. */ String script4 = "var startDate=\"3/30/07 8:01 PM\" ;var years=0;var months=1;var days=3;" + "DateTimeSpan.subDate(startDate,years,months,days);"; Object value4 = eval( script4 ); cal = getCalendarInstance( ); cal.setTime( (Date) value4 ); assertEquals( 2007, cal.get( Calendar.YEAR ) ); assertEquals( 1, cal.get( Calendar.MONTH ) ); assertEquals( 25, cal.get( Calendar.DATE ) ); System.out.println( "4" ); }
Example 18
Source File: NativeDateTimeSpanTest.java From birt with Eclipse Public License 1.0 | 4 votes |
@Test public void testHours( ) { /* * A whole day in the normal year */ String script1 = "var startDate=\"2/3/09 8:01 PM\" ;var endDate=\"2/4/09 8:01 PM\" ;DateTimeSpan.hours(startDate,endDate);"; Object value1 = eval( script1 ); if ( value1 instanceof Integer ) assertEquals( 24, ( (Integer) value1 ).intValue( ) ); /* * A whole day in a leap year */ String script3 = "var startDate=\"2/29/08 8:01 PM\" ;var endDate=\"3/1/08 8:01 PM\" ;DateTimeSpan.hours(startDate,endDate);"; Object value3 = eval( script3 ); if ( value3 instanceof Integer ) assertEquals( 24, ( (Integer) value3 ).intValue( ) ); /* * In the same day, less than an hour, equal to an hour, more than an * hour */ String script2 = "var startDate=\"3/1/08 8:01 PM\" ;var endDate=\"3/1/08 9:02 PM\" ;DateTimeSpan.hours(startDate,endDate);"; Object value2 = eval( script2 ); if ( value2 instanceof Integer ) assertEquals( 1, ( (Integer) value2 ).intValue( ) ); String script4 = "var startDate=\"2/2/08 8:01 PM\" ;var endDate=\"2/2/08 9:00 PM\" ;DateTimeSpan.hours(startDate,endDate);"; Object value4 = eval( script4 ); if ( value4 instanceof Integer ) assertEquals( 0, ( (Integer) value4 ).intValue( ) ); String script5 = "var startDate=\"2/2/08 8:01 PM\" ;var endDate=\"2/2/08 9:02 PM\" ;DateTimeSpan.hours(startDate,endDate);"; Object value5 = eval( script5 ); if ( value5 instanceof Integer ) assertEquals( 1, ( (Integer) value5 ).intValue( ) ); /* * More than one day. */ String script6 = "var startDate=\"2/2/08 8:01 PM\" ;var endDate=\"2/3/08 9:02 PM\" ;DateTimeSpan.hours(startDate,endDate);"; Object value6 = eval( script6 ); if ( value6 instanceof Integer ) assertEquals( 25, ( (Integer) value6 ).intValue( ) ); /* * Just one minute off a whole day. */ String script7 = "var startDate=\"2/2/08 8:03 PM\" ;var endDate=\"2/3/08 8:02 PM\" ;DateTimeSpan.hours(startDate,endDate);"; Object value7 = eval( script7 ); if ( value7 instanceof Integer ) assertEquals( 23, ( (Integer) value7 ).intValue( ) ); /* * More than one year, a normal year. */ String script8 = "var startDate=\"2/2/09 8:01 PM\" ;var endDate=\"2/2/10 8:01 PM\" ;DateTimeSpan.hours(startDate,endDate);"; Object value8 = eval( script8 ); if ( value8 instanceof Integer ) assertEquals( 8760, ( (Integer) value8 ).intValue( ) ); /* * More than one yare, a leap year. */ String script9 = "var startDate=\"2/2/08 8:01 PM\" ;var endDate=\"2/3/09 8:00 PM\" ;DateTimeSpan.hours(startDate,endDate);"; Object value9 = eval( script9 ); if ( value9 instanceof Integer ) assertEquals( 8807, ( (Integer) value9 ).intValue( ) ); /* * The first hour is more than the second one. */ String script10 = "var startDate=\"2/2/09 9:01 PM\" ;var endDate=\"2/3/09 8:00 PM\" ;DateTimeSpan.hours(startDate,endDate);"; Object value10 = eval( script10 ); if ( value10 instanceof Integer ) assertEquals( 22, ( (Integer) value10 ).intValue( ) ); /* * The first hour is less than the second one. */ String script11 = "var startDate=\"2/2/09 3:01 PM\" ;var endDate=\"2/3/09 8:00 PM\" ;DateTimeSpan.hours(startDate,endDate);"; Object value11 = eval( script11 ); if ( value11 instanceof Integer ) assertEquals( 28, ( (Integer) value11 ).intValue( ) ); /* * Use Date instance to test this method */ Calendar dateCal1 = getCalendarInstance( ); dateCal1.set( 2008, 8, 13, 20, 1, 44 ); Date date1 = dateCal1.getTime( ); Object jsNumber1 = Context.javaToJS( date1, scope ); ScriptableObject.putProperty( scope, "date1", jsNumber1 ); dateCal1 = getCalendarInstance( ); dateCal1.set( 2008, 8, 13, 21, 1, 44 ); Date date2 = dateCal1.getTime( ); Object jsNumber2 = Context.javaToJS( date2, scope ); ScriptableObject.putProperty( scope, "date2", jsNumber2 ); String script12 = "DateTimeSpan.hours(date1,date2)"; Object value12 = eval( script12 ); if ( value12 instanceof Integer ) { assertEquals( 1, ( (Integer) value12 ).intValue( ) ); } }
Example 19
Source File: Require.java From astor with GNU General Public License v2.0 | 4 votes |
private static void defineReadOnlyProperty(ScriptableObject obj, String name, Object value) { ScriptableObject.putProperty(obj, name, value); obj.setAttributes(name, ScriptableObject.READONLY | ScriptableObject.PERMANENT); }
Example 20
Source File: Require.java From JsDroidCmd with Mozilla Public License 2.0 | 4 votes |
private static void defineReadOnlyProperty(ScriptableObject obj, String name, Object value) { ScriptableObject.putProperty(obj, name, value); obj.setAttributes(name, ScriptableObject.READONLY | ScriptableObject.PERMANENT); }