@Override public void runTest(TestRun run) throws IOException { if (++runs == 50) { runs = 0; cleanUp(); init(); } try { page.set(webClient.get().getPage("about:blank")); } catch (IOException e) { throw new RuntimeException(e); } HtmlPage pageRef = page.get(); pageRef.executeJavaScript(readFile(new File(run.getBaseDirectory(), run.getFileName()))); boolean decodeStack = Boolean.parseBoolean(System.getProperty(TeaVMTestRunner.JS_DECODE_STACK, "true")); File debugFile = decodeStack ? new File(run.getBaseDirectory(), run.getFileName() + ".teavmdbg") : null; RhinoResultParser resultParser = new RhinoResultParser(debugFile); AsyncResult asyncResult = new AsyncResult(); Function function = (Function) page.get().executeJavaScript(readResource("teavm-htmlunit-adapter.js")) .getJavaScriptResult(); Object[] args = new Object[] { decodeStack ? createStackDecoderFunction(resultParser) : null, new NativeJavaObject(function, asyncResult, AsyncResult.class) }; pageRef.executeJavaScriptFunction(function, function, args, page.get()); resultParser.parseResult((Scriptable) asyncResult.getResult(), run.getCallback()); }
public static Scriptable createAdapterWrapper(Scriptable obj, Object adapter) { Scriptable scope = ScriptableObject.getTopLevelScope(obj); NativeJavaObject res = new NativeJavaObject(scope, adapter, null, true); res.setPrototype(obj); return res; }
switch (getJSTypeCode(value)) { reportConversionError(value, type); reportConversionError("undefined", type); reportConversionError(value, type); return coerceToNumber(Double.TYPE, value); return coerceToNumber(type, value); reportConversionError(value, type); return coerceToNumber(type, value); return coerceToNumber(type, value); reportConversionError(value, type); reportConversionError(value, type); reportConversionError(value, type); return coerceToNumber(type, value); reportConversionError(value, type); reportConversionError(value, type);
@Override public Object get(String id, Scriptable start) { if (id.equals("length")) return Integer.valueOf(length); Object result = super.get(id, start); if (result == NOT_FOUND && !ScriptableObject.hasProperty(getPrototype(), id)) { throw Context.reportRuntimeError2( "msg.java.member.not.found", array.getClass().getName(), id); } return result; }
int fromCode = getJSTypeCode(fromObj); return 1 + getSizeRank(to); ? CONVERSION_NONE : 2 + getSizeRank(to); return 3 + getSizeRank(to);
/** * Determine whether we can/should convert between the given type and the * desired one. This should be superceded by a conversion-cost calculation * function, but for now I'll hide behind precedent. */ public static boolean canConvert(Object fromObj, Class<?> to) { int weight = getConversionWeight(fromObj, to); return (weight < CONVERSION_NONE); }
/** * Not intended for public use. Callers should use the * public API Context.toType. * @deprecated as of 1.5 Release 4 * @see net.sourceforge.htmlunit.corejs.javascript.Context#jsToJava(Object, Class) */ public static Object coerceType(Class<?> type, Object value) { return coerceTypeImpl(type, value); }
if (!NativeJavaObject.canConvert(args[j], argTypes[j])) { if (debug) printDebug("Rejecting (args can't convert) ", member, args); if (!NativeJavaObject.canConvert(args[j], argTypes[j])) { if (debug) printDebug("Rejecting (args can't convert) ", member, args);
/** * {@inheritDoc} */ @Override public Object get(final String name, final Scriptable start) { // for java mocks do a bit more, we have handle unknown properties // ourself if (wrappedActiveX_ instanceof NativeJavaObject) { final NativeJavaObject obj = (NativeJavaObject) wrappedActiveX_; final Object result = obj.get(name, start); if (Scriptable.NOT_FOUND != result) { return result; } return super.get(name, start); } if (wrappedActiveX_ != null) { return wrappedActiveX_.get(name, start); } return super.get(name, start); }
int rank1 = NativeJavaObject.getConversionWeight(arg, type1); int rank2 = NativeJavaObject.getConversionWeight(arg, type2);
/** * Convert a JavaScript value into the desired type. * Uses the semantics defined with LiveConnect3 and throws an * Illegal argument exception if the conversion cannot be performed. * @param value the JavaScript value to convert * @param desiredType the Java type to convert to. Primitive Java * types are represented using the TYPE fields in the corresponding * wrapper class in java.lang. * @return the converted value * @throws EvaluatorException if the conversion cannot be performed */ public static Object jsToJava(Object value, Class<?> desiredType) throws EvaluatorException { return NativeJavaObject.coerceTypeImpl(desiredType, value); }
/** * Wrap Java object as Scriptable instance to allow full access to its * methods and fields from JavaScript. * <p> * {@link #wrap(Context, Scriptable, Object, Class)} and * {@link #wrapNewObject(Context, Scriptable, Object)} call this method * when they can not convert <tt>javaObject</tt> to JavaScript primitive * value or JavaScript array. * <p> * Subclasses can override the method to provide custom wrappers * for Java objects. * @param cx the current Context for this thread * @param scope the scope of the executing script * @param javaObject the object to be wrapped * @param staticType type hint. If security restrictions prevent to wrap object based on its class, staticType will be used instead. * @return the wrapped value which shall not be null */ public Scriptable wrapAsJavaObject(Context cx, Scriptable scope, Object javaObject, Class<?> staticType) { Scriptable wrap; wrap = new NativeJavaObject(scope, javaObject, staticType); return wrap; }
/** * {@inheritDoc} */ @Override public Object get(final String name, final Scriptable start) { // for java mocks do a bit more, we have handle unknown properties // ourself if (wrappedActiveX_ instanceof NativeJavaObject) { final NativeJavaObject obj = (NativeJavaObject) wrappedActiveX_; final Object result = obj.get(name, start); if (Scriptable.NOT_FOUND != result) { return result; } return super.get(name, start); } if (wrappedActiveX_ != null) { return wrappedActiveX_.get(name, start); } return super.get(name, start); }
@Override public String runTest(TestRun run) throws IOException { if (++runs == 50) { runs = 0; cleanUp(); init(); } try { page.set(webClient.get().<HtmlPage>getPage("about:blank")); } catch (IOException e) { throw new RuntimeException(e); } page.get().executeJavaScript(readFile(new File(run.getBaseDirectory(), "runtime.js"))); page.get().executeJavaScript(readFile(new File(run.getBaseDirectory(), "test.js"))); AsyncResult asyncResult = new AsyncResult(); Function function = (Function) page.get().executeJavaScript(readResource("teavm-htmlunit-adapter.js")) .getJavaScriptResult(); Object[] args = new Object[] { new NativeJavaObject(function, asyncResult, AsyncResult.class) }; page.get().executeJavaScriptFunctionIfPossible(function, function, args, page.get()); return (String) asyncResult.getResult(); }
throw Context.reportRuntimeError0("msg.default.value"); Object converterObject = get(converterName, this); if (converterObject instanceof Function) { Function f = (Function)converterObject;
function.put("host", function, new NativeJavaObject(function, asyncResult, null));