123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- using Esprima.Ast;
- using Jint.Native.Object;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Descriptors.Specialized;
- using Jint.Runtime.Environments;
- using Jint.Runtime.Interpreter;
- namespace Jint.Native.Function
- {
- public sealed class ScriptFunctionInstance : FunctionInstance, IConstructor
- {
- internal bool _isClassConstructor;
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
- /// </summary>
- public ScriptFunctionInstance(
- Engine engine,
- IFunction functionDeclaration,
- EnvironmentRecord scope,
- bool strict,
- ObjectInstance? proto = null)
- : this(
- engine,
- new JintFunctionDefinition(engine, functionDeclaration),
- scope,
- strict ? FunctionThisMode.Strict : FunctionThisMode.Global,
- proto)
- {
- }
- internal ScriptFunctionInstance(
- Engine engine,
- JintFunctionDefinition function,
- EnvironmentRecord scope,
- FunctionThisMode thisMode,
- ObjectInstance? proto = null)
- : base(engine, engine.Realm, function, scope, thisMode)
- {
- _prototype = proto ?? _engine.Realm.Intrinsics.Function.PrototypeObject;
- _length = new LazyPropertyDescriptor(null, _ => JsNumber.Create(function.Initialize(this).Length), PropertyFlag.Configurable);
- if (!function.Strict
- && !engine._isStrict
- && function.Function is not ArrowFunctionExpression
- && !function.Function.Generator)
- {
- SetProperty(KnownKeys.Arguments, new GetSetPropertyDescriptor.ThrowerPropertyDescriptor(engine, PropertyFlag.Configurable | PropertyFlag.CustomJsValue));
- SetProperty(KnownKeys.Caller, new PropertyDescriptor(Undefined, PropertyFlag.Configurable));
- }
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
- /// </summary>
- protected internal override JsValue Call(JsValue thisArgument, JsValue[] arguments)
- {
- var strict = _thisMode == FunctionThisMode.Strict || _engine._isStrict;
- using (new StrictModeScope(strict, true))
- {
- try
- {
- var calleeContext = PrepareForOrdinaryCall(Undefined);
- if (_isClassConstructor)
- {
- ExceptionHelper.ThrowTypeError(calleeContext.Realm, $"Class constructor {_functionDefinition.Name} cannot be invoked without 'new'");
- }
- OrdinaryCallBindThis(calleeContext, thisArgument);
- // actual call
- var context = _engine._activeEvaluationContext ?? new EvaluationContext(_engine);
- var result = _functionDefinition.EvaluateBody(context, this, arguments);
- if (result.Type == CompletionType.Throw)
- {
- ExceptionHelper.ThrowJavaScriptException(_engine, result.Value, result);
- }
- // The DebugHandler needs the current execution context before the return for stepping through the return point
- if (context.DebugMode)
- {
- // We don't have a statement, but we still need a Location for debuggers. DebugHandler will infer one from
- // the function body:
- _engine.DebugHandler.OnReturnPoint(
- _functionDefinition.Function.Body,
- result.Type == CompletionType.Normal ? Undefined : result.Value
- );
- }
- if (result.Type == CompletionType.Return)
- {
- return result.Value;
- }
- }
- finally
- {
- _engine.LeaveExecutionContext();
- }
- return Undefined;
- }
- }
- internal override bool IsConstructor =>
- (_homeObject.IsUndefined() || _isClassConstructor)
- && _functionDefinition?.Function is not ArrowFunctionExpression
- && _functionDefinition?.Function?.Generator != true;
- /// <summary>
- /// https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget
- /// </summary>
- ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
- {
- var callerContext = _engine.ExecutionContext;
- var kind = _constructorKind;
- var thisArgument = Undefined;
- if (kind == ConstructorKind.Base)
- {
- thisArgument = OrdinaryCreateFromConstructor(
- newTarget,
- static intrinsics => intrinsics.Object.PrototypeObject,
- static (Engine engine, Realm _, object? _) => new ObjectInstance(engine));
- }
- var calleeContext = PrepareForOrdinaryCall(newTarget);
- if (kind == ConstructorKind.Base)
- {
- OrdinaryCallBindThis(calleeContext, thisArgument);
- InitializeInstanceElements(thisArgument, this);
- }
- var constructorEnv = (FunctionEnvironmentRecord) calleeContext.LexicalEnvironment;
- var strict = _thisMode == FunctionThisMode.Strict;
- using (new StrictModeScope(strict, force: true))
- {
- try
- {
- var context = _engine._activeEvaluationContext ?? new EvaluationContext(_engine);
- var result = _functionDefinition.EvaluateBody(context, this, arguments);
- // The DebugHandler needs the current execution context before the return for stepping through the return point
- if (context.DebugMode && result.Type != CompletionType.Throw)
- {
- // We don't have a statement, but we still need a Location for debuggers. DebugHandler will infer one from
- // the function body:
- _engine.DebugHandler.OnReturnPoint(
- _functionDefinition.Function.Body,
- result.Type == CompletionType.Normal ? thisArgument : result.Value
- );
- }
- if (result.Type == CompletionType.Return)
- {
- if (result.Value is ObjectInstance oi)
- {
- return oi;
- }
- if (kind == ConstructorKind.Base)
- {
- return (ObjectInstance) thisArgument;
- }
- if (!result.Value.IsUndefined())
- {
- ExceptionHelper.ThrowTypeError(callerContext.Realm);
- }
- }
- else if (result.Type == CompletionType.Throw)
- {
- ExceptionHelper.ThrowJavaScriptException(_engine, result.Value, result);
- }
- }
- finally
- {
- _engine.LeaveExecutionContext();
- }
- }
- return (ObjectInstance) constructorEnv.GetThisBinding();
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-initializeinstanceelements
- /// </summary>
- private void InitializeInstanceElements(JsValue o, JsValue constructor)
- {
- // TODO private fields
- }
- internal void MakeClassConstructor()
- {
- _isClassConstructor = true;
- }
- }
- }
|