use of com.github.anba.es6draft.runtime.types.Constructor in project es6draft by anba.
the class FunctionCodeGenerator method generateLegacyFunctionConstruct.
/**
* Generate bytecode for:
*
* <pre>
* oldCaller = function.getLegacyCaller()
* oldArguments = function.getLegacyArguments()
* function.setLegacyCaller(callerContext.getCurrentFunction())
* try {
* thisArgument = OrdinaryCreateFromConstructor(callerContext, newTarget, %ObjectPrototype%)
* calleeContext = newFunctionExecutionContext(function, newTarget, thisArgument)
* result = OrdinaryCallEvaluateBody(function, argumentsList)
* return returnResultOrThis(result)
* } finally {
* function.restoreLegacyProperties(oldCaller, oldArguments)
* }
* </pre>
*
* @param node
* the function node
* @param mv
* the instruction visitor
*/
private void generateLegacyFunctionConstruct(FunctionNode node, InstructionVisitor mv) {
final boolean hasArguments = codegen.isEnabled(CompatibilityOption.FunctionArguments);
final boolean hasCaller = codegen.isEnabled(CompatibilityOption.FunctionCaller);
Variable<LegacyConstructorFunction> function = mv.getParameter(FUNCTION, LegacyConstructorFunction.class);
Variable<ExecutionContext> callerContext = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
Variable<Constructor> newTarget = mv.getParameter(NEW_TARGET, Constructor.class);
Variable<Object[]> arguments = mv.getParameter(ARGUMENTS, Object[].class);
Variable<ScriptObject> thisArg = mv.newVariable("thisArgument", ScriptObject.class);
Variable<ExecutionContext> calleeContext = mv.newVariable("calleeContext", ExecutionContext.class);
Variable<FunctionObject> oldCaller = mv.newVariable("oldCaller", FunctionObject.class);
Variable<LegacyConstructorFunction.Arguments> oldArguments = mv.newVariable("oldArguments", LegacyConstructorFunction.Arguments.class);
Variable<Throwable> throwable = mv.newVariable("throwable", Throwable.class);
// (1) Retrieve 'caller' and 'arguments' and store in local variables
if (hasCaller) {
mv.load(function);
mv.invoke(Methods.LegacyConstructorFunction_getLegacyCaller);
} else {
mv.anull();
}
mv.store(oldCaller);
if (hasArguments) {
mv.load(function);
mv.invoke(Methods.LegacyConstructorFunction_getLegacyArguments);
} else {
mv.anull();
}
mv.store(oldArguments);
// (2) Update 'caller' and 'arguments' properties
if (hasCaller) {
setLegacyCaller(function, callerContext, mv);
}
if (hasArguments) {
setLegacyArguments(function, arguments, mv);
}
TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
TryCatchLabel handlerFinally = new TryCatchLabel();
mv.mark(startFinally);
{
// (3) Create this-argument
ordinaryCreateFromConstructor(callerContext, newTarget, thisArg, mv);
// (4) Create a new ExecutionContext
prepareCallAndBindThis(node, calleeContext, function, newTarget, thisArg, mv);
// (5) Call OrdinaryCallEvaluateBody
ordinaryCallEvaluateBody(node, calleeContext, function, arguments, mv);
// (6) Restore 'caller' and 'arguments'
restoreLegacyProperties(function, oldCaller, oldArguments, mv);
// (7) Return result value
returnResultOrThis(thisArg, false, mv);
}
mv.mark(endFinally);
// Exception: Restore 'caller' and 'arguments' and then rethrow exception
mv.finallyHandler(handlerFinally);
mv.store(throwable);
restoreLegacyProperties(function, oldCaller, oldArguments, mv);
mv.load(throwable);
mv.athrow();
mv.tryFinally(startFinally, endFinally, handlerFinally);
}
use of com.github.anba.es6draft.runtime.types.Constructor in project es6draft by anba.
the class NodeModuleLoader method initialize.
/**
* Initializes this module loader.
*
* @param realm
* the realm instance
* @throws IOException
* if there was any I/O error
* @throws URISyntaxException
* the URL is not a valid URI
* @throws MalformedNameException
* if any imported module request cannot be normalized
* @throws ResolutionException
* if any export binding cannot be resolved
*/
public void initialize(Realm realm) throws IOException, URISyntaxException, MalformedNameException, ResolutionException {
ModuleRecord module = NativeCode.loadModule(realm, "module.jsm");
Constructor moduleConstructor = NativeCode.getModuleExport(module, "default", Constructor.class);
setModuleConstructor(moduleConstructor);
}
use of com.github.anba.es6draft.runtime.types.Constructor in project es6draft by anba.
the class TypedArrayConstructorPrototype method construct.
/**
* 22.2.1.1 %TypedArray% ( )
*/
@Override
public ScriptObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
ExecutionContext calleeContext = calleeContext();
/* step 3 */
if (newTarget == this) {
throw newTypeError(calleeContext, Messages.Key.TypedArrayCreate);
}
/* step 4 */
ScriptObject super_ = getPrototypeOf(calleeContext);
/* step 5 */
if (!IsConstructor(super_)) {
throw newTypeError(calleeContext, Messages.Key.NotConstructor);
}
/* steps 6-7 */
return ((Constructor) super_).construct(calleeContext, newTarget, args);
}
use of com.github.anba.es6draft.runtime.types.Constructor in project es6draft by anba.
the class ArrayBufferConstructor method CloneArrayBuffer.
/**
* 24.1.1.4 CloneArrayBuffer (srcBuffer, srcByteOffset)
*
* @param cx
* the execution context
* @param srcBuffer
* the source buffer
* @param srcByteOffset
* the source offset
* @param cloneConstructor
* the intrinsic constructor function
* @return the new array buffer object
*/
public static ArrayBufferObject CloneArrayBuffer(ExecutionContext cx, ArrayBuffer srcBuffer, long srcByteOffset, Intrinsics cloneConstructor) {
/* step 1 (implicit) */
/* steps 2-3 */
Constructor bufferConstructor;
if (cloneConstructor == null) {
/* steps 2.a-b */
bufferConstructor = SpeciesConstructor(cx, srcBuffer, Intrinsics.ArrayBuffer);
/* step 2.c */
if (IsDetachedBuffer(srcBuffer)) {
throw newTypeError(cx, Messages.Key.BufferDetached);
}
} else {
/* step 3 */
assert IsConstructor(cx.getIntrinsic(cloneConstructor));
bufferConstructor = (Constructor) cx.getIntrinsic(cloneConstructor);
}
/* step 4 */
ByteBuffer srcBlock = srcBuffer.getData();
/* step 5 */
long srcLength = srcBuffer.getByteLength();
/* step 6 */
assert srcByteOffset <= srcLength;
/* step 7 */
long cloneLength = srcLength - srcByteOffset;
/* steps 8-9 */
ArrayBufferObject targetBuffer = AllocateArrayBuffer(cx, bufferConstructor, cloneLength);
/* step 10 */
if (IsDetachedBuffer(srcBuffer)) {
throw newTypeError(cx, Messages.Key.BufferDetached);
}
/* step 11 */
ByteBuffer targetBlock = targetBuffer.getData();
/* step 12 */
CopyDataBlockBytes(targetBlock, 0, srcBlock, srcByteOffset, cloneLength);
/* step 13 */
return targetBuffer;
}
use of com.github.anba.es6draft.runtime.types.Constructor in project es6draft by anba.
the class PropertiesTest method createCustomClassWithSubclass.
@Test
public void createCustomClassWithSubclass() {
Constructor customClass = Properties.createClass(cx, "CustomClassWithSubclass", CustomClassWithSubclass.ConstructorProperties.class, CustomClassWithSubclass.PrototypeProperties.class);
ScriptObject object = customClass.construct(cx);
assertNotNull(object);
}
Aggregations