/// <summary> 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. /// </summary> public virtual IScriptable CreateObject(Context cx, IScriptable scope) { IScriptable newInstance = new BuiltinObject(); newInstance.SetPrototype(GetClassPrototype()); newInstance.ParentScope = ParentScope; return(newInstance); }
public static void ImportType(IScriptable scope, Type type) { if (!type.IsPublic) { return; } if (ScriptRuntime.IsNativeRuntimeType(type)) { return; } // Cannot define 'Object' if (type.Name == "Object") { return; } string [] ns = type.FullName.Split('.'); IScriptable parent = scope; for (int i = 0; i < ns.Length - 1; i++) { IScriptable obj = (parent.Get(ns [i], parent) as IScriptable); if (obj == null) { obj = new BuiltinObject(); parent.Put(ns [i], parent, obj); } parent = obj; } object thisObj = null; if (type.IsEnum) { thisObj = new CliEnum((Enum)Activator.CreateInstance(type)); } else { thisObj = CliType.GetNativeCliType(type); } // Define as toplevel object scope.Put(ns [ns.Length - 1], scope, thisObj); // Define as full qualified name parent.Put(ns [ns.Length - 1], parent, thisObj); }
private void SetupDefaultPrototype() { BuiltinObject obj = new BuiltinObject(); obj.DefineProperty("constructor", this, ScriptableObject.DONTENUM); // 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; IScriptable proto = GetObjectPrototype(this); if (proto != obj) { // not the one we just made, it must remain grounded obj.SetPrototype(proto); } }
public static void ImportType(IScriptable scope, Type type) { if (!type.IsPublic) return; if (ScriptRuntime.IsNativeRuntimeType (type)) return; // Cannot define 'Object' if (type.Name == "Object") return; string [] ns = type.FullName.Split ('.'); IScriptable parent = scope; for (int i = 0; i < ns.Length - 1; i++) { IScriptable obj = (parent.Get (ns [i], parent) as IScriptable); if (obj == null) { obj = new BuiltinObject (); parent.Put (ns [i], parent, obj); } parent = obj; } object thisObj = null; if (type.IsEnum) { thisObj = new CliEnum ((Enum)Activator.CreateInstance (type)); } else { thisObj = CliType.GetNativeCliType (type); } // Define as toplevel object scope.Put (ns [ns.Length - 1], scope, thisObj); // Define as full qualified name parent.Put (ns [ns.Length - 1], parent, thisObj); }
/// <summary> Note that if the <code>delegee</code> is <code>null</code>, /// this method creates a new instance of the Delegator itself /// rathert than forwarding the call to the /// <code>delegee</code>. This permits the use of Delegator /// prototypes. /// /// </summary> /// <param name="cx">the current Context for this thread /// </param> /// <param name="scope">an enclosing scope of the caller except /// when the function is called from a closure. /// </param> /// <param name="args">the array of arguments /// </param> /// <returns> the allocated object /// /// </returns> public virtual IScriptable Construct(Context cx, IScriptable scope, object [] args) { if (obj == null) { //this little trick allows us to declare prototype objects for //Delegators Delegator n = NewInstance(); IScriptable delegee; if (args.Length == 0) { delegee = new BuiltinObject(); } else { delegee = ScriptConvert.ToObject(cx, scope, args [0]); } n.Delegee = delegee; return(n); } else { return(((IFunction)obj).Construct(cx, scope, args)); } }