📄 basefunction.java
字号:
return (BaseFunction)x; } throw ScriptRuntime.typeError1("msg.incompat.call", f.getFunctionName()); } /** * Make value as DontEnum, DontDelete, ReadOnly * prototype property of this Function object */ public void setImmunePrototypeProperty(Object value) { if (isPrototypePropertyImmune) { throw new IllegalStateException(); } prototypeProperty = (value != null) ? value : UniqueTag.NULL_VALUE; isPrototypePropertyImmune = true; } protected Scriptable getClassPrototype() { Object protoVal = getPrototypeProperty(); if (protoVal instanceof Scriptable) { return (Scriptable) protoVal; } return getClassPrototype(this, "Object"); } /** * Should be overridden. */ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { return Undefined.instance; } public Scriptable construct(Context cx, Scriptable scope, Object[] args) { Scriptable result = createObject(cx, scope); if (result != null) { Object val = call(cx, scope, result, args); if (val instanceof Scriptable) { result = (Scriptable)val; } } else { Object val = call(cx, scope, null, args); if (!(val instanceof Scriptable)) { // It is program error not to return Scriptable from // the call method if createObject returns null. throw new IllegalStateException( "Bad implementaion of call as constructor, name=" +getFunctionName()+" in "+getClass().getName()); } result = (Scriptable)val; if (result.getPrototype() == null) { result.setPrototype(getClassPrototype()); } if (result.getParentScope() == null) { Scriptable parent = getParentScope(); if (result != parent) { result.setParentScope(parent); } } } return result; } /** * Creates new script object. * The default implementation of {@link #construct} uses the method to * to get the value for <tt>thisObj</tt> argument when invoking * {@link #call}. * The methos is allowed to return <tt>null</tt> to indicate that * {@link #call} will create a new object itself. In this case * {@link #construct} will set scope and prototype on the result * {@link #call} unless they are already set. */ public Scriptable createObject(Context cx, Scriptable scope) { Scriptable newInstance = new NativeObject(); newInstance.setPrototype(getClassPrototype()); newInstance.setParentScope(getParentScope()); return newInstance; } /** * Decompile the source information associated with this js * function/script back into a string. * * @param indent How much to indent the decompiled result. * * @param flags Flags specifying format of decompilation output. */ String decompile(int indent, int flags) { StringBuffer sb = new StringBuffer(); boolean justbody = (0 != (flags & Decompiler.ONLY_BODY_FLAG)); if (!justbody) { sb.append("function "); sb.append(getFunctionName()); sb.append("() {\n\t"); } sb.append("[native code, arity="); sb.append(getArity()); sb.append("]\n"); if (!justbody) { sb.append("}\n"); } return sb.toString(); } public int getArity() { return 0; } public int getLength() { return 0; } public String getFunctionName() { return ""; } final Object getPrototypeProperty() { Object result = prototypeProperty; if (result == null) { synchronized (this) { result = prototypeProperty; if (result == null) { setupDefaultPrototype(); result = prototypeProperty; } } } else if (result == UniqueTag.NULL_VALUE) { result = null; } return result; } private void setupDefaultPrototype() { NativeObject obj = new NativeObject(); final int attr = ScriptableObject.DONTENUM; obj.defineProperty("constructor", this, attr); // put the prototype property into the object now, then in the // wacky case of a user defining a function Object(), we don't // get an infinite loop trying to find the prototype. prototypeProperty = obj; Scriptable proto = getObjectPrototype(this); if (proto != obj) { // not the one we just made, it must remain grounded obj.setPrototype(proto); } } private Object getArguments() { // <Function name>.arguments is deprecated, so we use a slow // way of getting it that doesn't add to the invocation cost. // TODO: add warning, error based on version Object value = defaultGet("arguments"); if (value != NOT_FOUND) { // Should after changing <Function name>.arguments its // activation still be available during Function call? // This code assumes it should not: // defaultGet("arguments") != NOT_FOUND // means assigned arguments return value; } Context cx = Context.getContext(); NativeCall activation = ScriptRuntime.findFunctionActivation(cx, this); return (activation == null) ? null : activation.get("arguments", activation); } private static Object jsConstructor(Context cx, Scriptable scope, Object[] args) { int arglen = args.length; StringBuffer sourceBuf = new StringBuffer(); sourceBuf.append("function "); /* version != 1.2 Function constructor behavior - * print 'anonymous' as the function name if the * version (under which the function was compiled) is * less than 1.2... or if it's greater than 1.2, because * we need to be closer to ECMA. */ if (cx.getLanguageVersion() != Context.VERSION_1_2) { sourceBuf.append("anonymous"); } sourceBuf.append('('); // Append arguments as coma separated strings for (int i = 0; i < arglen - 1; i++) { if (i > 0) { sourceBuf.append(','); } sourceBuf.append(ScriptRuntime.toString(args[i])); } sourceBuf.append(") {"); if (arglen != 0) { // append function body String funBody = ScriptRuntime.toString(args[arglen - 1]); sourceBuf.append(funBody); } sourceBuf.append('}'); String source = sourceBuf.toString(); int[] linep = new int[1]; String filename = Context.getSourcePositionFromStack(linep); if (filename == null) { filename = "<eval'ed string>"; linep[0] = 1; } String sourceURI = ScriptRuntime. makeUrlForGeneratedScript(false, filename, linep[0]); Scriptable global = ScriptableObject.getTopLevelScope(scope); ErrorReporter reporter; reporter = DefaultErrorReporter.forEval(cx.getErrorReporter()); // Compile with explicit interpreter instance to force interpreter // mode. return cx.compileFunction(global, source, new Interpreter(), reporter, sourceURI, 1, null); } protected int findPrototypeId(String s) { int id;// #string_id_map#// #generated# Last update: 2004-03-17 13:23:22 CET L0: { id = 0; String X = null; int c; L: switch (s.length()) { case 4: X="call";id=Id_call; break L; case 5: X="apply";id=Id_apply; break L; case 8: c=s.charAt(3); if (c=='o') { X="toSource";id=Id_toSource; } else if (c=='t') { X="toString";id=Id_toString; } break L; case 11: X="constructor";id=Id_constructor; break L; } if (X!=null && X!=s && !X.equals(s)) id = 0; }// #/generated# return id; } private static final int Id_constructor = 1, Id_toString = 2, Id_toSource = 3, Id_apply = 4, Id_call = 5, MAX_PROTOTYPE_ID = 5;// #/string_id_map# private Object prototypeProperty; private boolean isPrototypePropertyImmune;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -