private static Scriptable getTopScope(final Scriptable s) { Scriptable top = s; while (top != null && top.getParentScope() != null) { top = top.getParentScope(); } return top; }
/** * @see net.sourceforge.htmlunit.corejs.javascript.Scriptable#getParentScope */ public Scriptable getParentScope() { return obj.getParentScope(); } /**
private static Scriptable getTopScope(final Scriptable s) { Scriptable top = s; while (top != null && top.getParentScope() != null) { top = top.getParentScope(); } return top; }
private static Scriptable getTopScope(final Scriptable s) { Scriptable top = s; while (top != null && top.getParentScope() != null) { top = top.getParentScope(); } return top; }
/** * Get the global scope. * * <p>Walks the parent scope chain to find an object with a null * parent scope (the global object). * * @param obj a JavaScript object * @return the corresponding global scope */ public static Scriptable getTopLevelScope(Scriptable obj) { for (;;) { Scriptable parent = obj.getParentScope(); if (parent == null) { return obj; } obj = parent; } }
/** * Gets the browser version currently used. * @return the browser version */ public BrowserVersion getBrowserVersion() { Scriptable parent = getParentScope(); while (!(parent instanceof Window)) { parent = parent.getParentScope(); } return ((Window) parent).getBrowserVersion(); }
/** * Gets the browser version currently used. * @return the browser version */ public BrowserVersion getBrowserVersion() { Scriptable parent = getParentScope(); while (!(parent instanceof Window)) { parent = parent.getParentScope(); } return ((Window) parent).getBrowserVersion(); }
/** * Returns a string with a language sensitive representation of this number. * @param context the JavaScript context * @param thisObj the scriptable * @param args the arguments passed into the method * @param function the function * @return the string */ public static String toLocaleString( final Context context, final Scriptable thisObj, final Object[] args, final Function function) { if (args.length != 0 && args[0] instanceof String) { final String localeStr = (String) args[0]; try { final Locale locale = new Locale.Builder().setLanguageTag(localeStr).build(); return NumberFormat.getInstance(locale).format(Double.parseDouble(thisObj.toString())); } catch (final IllformedLocaleException e) { throw ScriptRuntime.rangeError("Invalid language tag: " + localeStr); } } final BrowserVersion browserVersion = ((Window) thisObj.getParentScope()).getBrowserVersion(); final Locale locale = Locale.forLanguageTag(browserVersion.getBrowserLanguage()); return NumberFormat.getInstance(locale).format(Double.parseDouble(thisObj.toString())); } }
private Object js_eval(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { if (thisObj.getParentScope() == null) { // We allow indirect calls to eval as long as the script will execute in // the global scope. return ScriptRuntime.evalSpecial(cx, scope, thisObj, args, "eval code", 1); } String m = ScriptRuntime.getMessage1("msg.cant.call.indirect", "eval"); throw NativeGlobal.constructError(cx, "EvalError", m, scope); }
@Override public Object get(Context cx) { switch (type) { case SPECIAL_NONE: return ScriptRuntime.getObjectProp(target, name, cx); case SPECIAL_PROTO: return target.getPrototype(); case SPECIAL_PARENT: return target.getParentScope(); default: throw Kit.codeBug(); } }
/** * Looks up a name in the scope chain and returns its value. */ public static Object name(Context cx, Scriptable scope, String name) { Scriptable parent = scope.getParentScope(); if (parent == null) { Object result = topScopeName(cx, scope, name); if (result == Scriptable.NOT_FOUND) { throw notFoundError(scope, name); } return result; } return nameOrFunction(cx, scope, parent, name, false); }
public static Object searchDefaultNamespace(Context cx) { Scriptable scope = cx.currentActivationCall; if (scope == null) { scope = getTopCallScope(cx); } Object nsObject; for (;;) { Scriptable parent = scope.getParentScope(); if (parent == null) { nsObject = ScriptableObject.getProperty(scope, DEFAULT_NS_TAG); if (nsObject == Scriptable.NOT_FOUND) { return null; } break; } nsObject = scope.get(DEFAULT_NS_TAG, scope); if (nsObject != Scriptable.NOT_FOUND) { break; } scope = parent; } return nsObject; }
public static Object nameIncrDecr(Scriptable scopeChain, String id, Context cx, int incrDecrMask) { Scriptable target; Object value; search: { do { if (cx.useDynamicScope && scopeChain.getParentScope() == null) { scopeChain = checkDynamicScope(cx.topCallScope, scopeChain); } target = scopeChain; do { value = target.get(id, scopeChain); if (value != Scriptable.NOT_FOUND) { break search; } target = target.getPrototype(); } while (target != null); scopeChain = scopeChain.getParentScope(); } while (scopeChain != null); throw notFoundError(scopeChain, id); } return doScriptableIncrDecr(target, id, scopeChain, value, incrDecrMask); }
public static Object enumInit(Object value, Context cx, int enumType) { IdEnumeration x = new IdEnumeration(); x.obj = toObjectOrNull(cx, value); if (x.obj == null) { // null or undefined do not cause errors but rather lead to empty // "for in" loop return x; } x.enumType = enumType; x.iterator = null; if (enumType != ENUMERATE_KEYS_NO_ITERATOR && enumType != ENUMERATE_VALUES_NO_ITERATOR && enumType != ENUMERATE_ARRAY_NO_ITERATOR) { x.iterator = toIterator(cx, x.obj.getParentScope(), x.obj, enumType == ScriptRuntime.ENUMERATE_KEYS); } if (x.iterator == null) { // enumInit should read all initial ids before returning // or "for (a.i in a)" would wrongly enumerate i in a as well enumChangeObject(x); } return x; }
/** * Converts a date to a string, returning the "time" portion using the current locale's conventions. * @param context the JavaScript context * @param thisObj the scriptable * @param args the arguments passed into the method * @param function the function * @return converted string */ public static String toLocaleTimeString( final Context context, final Scriptable thisObj, final Object[] args, final Function function) { final String formatString; final BrowserVersion browserVersion = ((Window) thisObj.getParentScope()).getBrowserVersion(); if (browserVersion.hasFeature(JS_DATE_WITH_LEFT_TO_RIGHT_MARK)) { // [U+200E] -> Unicode Character 'LEFT-TO-RIGHT MARK' formatString = "\u200Eh\u200E:\u200Emm\u200E:\u200Ess\u200E \u200Ea"; } else { formatString = "h:mm:ss a"; } final FastDateFormat format = FastDateFormat.getInstance(formatString, getLocale(browserVersion)); return format.format(getDateValue(thisObj)); }
private static Promise create(final Scriptable thisObj, final Object[] args, final PromiseState state) { // fulfilled promises are returned if (args.length != 0 && args[0] instanceof Promise && state == PromiseState.FULFILLED) { return (Promise) args[0]; } final Promise promise; if (args.length > 0) { final Object arg = args[0]; if (arg instanceof NativeObject) { final NativeObject nativeObject = (NativeObject) arg; promise = new Promise(nativeObject.get("then", nativeObject)); } else { promise = new Promise(); promise.value_ = arg; promise.state_ = state; } } else { promise = new Promise(); promise.value_ = Undefined.instance; promise.state_ = state; } promise.setParentScope(thisObj.getParentScope()); promise.setPrototype(getWindow(thisObj).getPrototype(promise.getClass())); return promise; }
/** * {@inheritDoc} */ @Override public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) { final Object object = super.call(cx, scope, thisObj, args); if (object instanceof Scriptable) { final Scriptable result = (Scriptable) object; if (result.getPrototype() == null) { final Scriptable proto = getClassPrototype(); if (result != proto) { result.setPrototype(proto); } } if (result.getParentScope() == null) { final Scriptable parent = getParentScope(); if (result != parent) { result.setParentScope(parent); } } } return object; } }
/** * Converts a date to a string, returning the "time" portion using the current locale's conventions. * @param context the JavaScript context * @param thisObj the scriptable * @param args the arguments passed into the method * @param function the function * @return converted string */ public static String toLocaleTimeString( final Context context, final Scriptable thisObj, final Object[] args, final Function function) { final String formatString; final BrowserVersion browserVersion = ((Window) thisObj.getParentScope()).getBrowserVersion(); if (browserVersion.hasFeature(JS_DATE_WITH_LEFT_TO_RIGHT_MARK)) { // [U+200E] -> Unicode Character 'LEFT-TO-RIGHT MARK' formatString = "\u200Eh\u200E:\u200Emm\u200E:\u200Ess\u200E \u200Ea"; } else { formatString = "h:mm:ss a"; } final FastDateFormat format = FastDateFormat.getInstance(formatString, getLocale(browserVersion)); return format.format(getDateValue(thisObj)); }
/** * {@inheritDoc} */ @Override public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) { final Object object = super.call(cx, scope, thisObj, args); if (object instanceof Scriptable) { final Scriptable result = (Scriptable) object; if (result.getPrototype() == null) { final Scriptable proto = getClassPrototype(); if (result != proto) { result.setPrototype(proto); } } if (result.getParentScope() == null) { final Scriptable parent = getParentScope(); if (result != parent) { result.setParentScope(parent); } } } return object; } }
/** * Converts a date to a string, returning the "date" portion using the operating system's locale's conventions. * @param context the JavaScript context * @param thisObj the scriptable * @param args the arguments passed into the method * @param function the function * @return converted string */ public static String toLocaleDateString( final Context context, final Scriptable thisObj, final Object[] args, final Function function) { final String formatString; final BrowserVersion browserVersion = ((Window) thisObj.getParentScope()).getBrowserVersion(); if (browserVersion.hasFeature(JS_DATE_WITH_LEFT_TO_RIGHT_MARK)) { // [U+200E] -> Unicode Character 'LEFT-TO-RIGHT MARK' formatString = "\u200EM\u200E/\u200Ed\u200E/\u200Eyyyy"; } else if (browserVersion.hasFeature(JS_DATE_LOCALE_DATE_SHORT)) { formatString = "M/d/yyyy"; } else { formatString = "EEEE, MMMM dd, yyyy"; } final FastDateFormat format = FastDateFormat.getInstance(formatString, getLocale(browserVersion)); return format.format(getDateValue(thisObj)); }