public static ObjectInstance Create(ScriptEngine engine, object prototype, [DefaultParameterValue(null)] object propertiesArg) { var properties = JurassicHelper.GetTypedArgumentValue <ObjectInstance>(engine, propertiesArg, null); if ((prototype is ObjectInstance) == false && prototype != Null.Value) { throw new JavaScriptException(engine, "TypeError", "object prototype must be an object or null"); } ObjectInstance result = prototype == Null.Value ? ObjectInstance.CreateRootObject(engine) : ObjectInstance.CreateRawObject((ObjectInstance)prototype); if (properties != null) { DefineProperties(result, properties); } return(result); }
/// <summary> /// /// </summary> /// <param name="engine"></param> /// <param name="name"></param> /// <param name="extends"></param> /// <param name="constructor"></param> /// <returns></returns> public static FunctionInstance ConstructClass(ScriptEngine engine, string name, object extends, UserDefinedFunction constructor) { if (extends is FunctionInstance extendsFunction) { // If extends doesn't have [[Construct]] then throw a TypeError. return(new ClassFunction(extendsFunction, name, ObjectInstance.CreateRawObject(extendsFunction.InstancePrototype), constructor)); } else if (extends == Null.Value) { return(new ClassFunction(engine.Function.InstancePrototype, name, ObjectInstance.CreateRootObject(engine), constructor)); } else { if (extends != null) { throw new JavaScriptException(ErrorType.TypeError, $"Class {name} cannot extend '{extends}' as it is not a constructor."); } return(new ClassFunction(engine.Function.InstancePrototype, name, engine.Object.Construct(), constructor)); } }
public ObjectInstance Construct() { return(ObjectInstance.CreateRawObject(this.InstancePrototype)); }