Chromium Code Reviews

Unified Diff: src/runtime.js

Issue 913073003: [es6] implement Reflect.apply() & Reflect.construct() (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Add Reflect.construct support Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Index: src/runtime.js
diff --git a/src/runtime.js b/src/runtime.js
index 7d82dfa8463fac3305ca98f42dd9a5545444a40c..b79b759e2483437386b764760b0a6867c4d50cf5 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -449,6 +449,93 @@ function APPLY_PREPARE(args) {
}
+function REFLECT_APPLY_PREPARE(args) {
+ var length;
+ // First check whether length is a positive Smi and args is an
+ // array. This is the fast case. If this fails, we do the slow case
+ // that takes care of more eventualities.
+ if (IS_ARRAY(args)) {
+ length = args.length;
+ if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
+ IS_SPEC_FUNCTION(this)) {
+ return length;
+ }
+ }
+
+ if (!IS_SPEC_FUNCTION(this)) {
+ throw %MakeTypeError('called_non_callable', [ %ToString(this) ]);
+ }
+
+ if (!IS_SPEC_OBJECT(args)) {
+ throw %MakeTypeError('reflect_apply_wrong_args', [ ]);
+ }
+
+ length = %ToLength(args.length);
+
+ // We can handle any number of apply arguments if the stack is
+ // big enough, but sanity check the value to avoid overflow when
+ // multiplying with pointer size.
+ if (length > 0x800000) {
arv (Not doing code reviews) 2015/03/03 14:25:52 maybe add a const/macro for this?
caitp (gmail) 2015/03/03 15:53:49 Replaced the magic number with a macro, hows that?
+ throw %MakeRangeError('stack_overflow', []);
+ }
+
+ // Return the length which is the number of arguments to copy to the
+ // stack. It is guaranteed to be a small integer at this point.
+ return length;
+}
+
+
+function REFLECT_CONSTRUCT_PREPARE(args, newTarget) {
+ var length;
+ var ctorOk = IS_SPEC_FUNCTION(this) && %IsConstructor(this);
+ var newTargetOk = IS_SPEC_FUNCTION(newTarget) && %IsConstructor(newTarget);
+
+ // First check whether length is a positive Smi and args is an
+ // array. This is the fast case. If this fails, we do the slow case
+ // that takes care of more eventualities.
+ if (IS_ARRAY(args)) {
+ length = args.length;
+ if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
+ ctorOk && newTargetOk) {
+ return length;
+ }
+ }
+
+ if (!ctorOk) {
+ if (!IS_SPEC_FUNCTION(this)) {
+ throw %MakeTypeError('called_non_callable', [ %ToString(this) ]);
+ } else {
+ throw %MakeTypeError('not_constructor', [ %ToString(this) ]);
+ }
+ }
+
+ if (!newTargetOk) {
+ if (!IS_SPEC_FUNCTION(newTarget)) {
+ throw %MakeTypeError('called_non_callable', [ %ToString(newTarget) ]);
+ } else {
+ throw %MakeTypeError('not_constructor', [ %ToString(newTarget) ]);
+ }
+ }
+
+ if (!IS_SPEC_OBJECT(args)) {
+ throw %MakeTypeError('reflect_construct_wrong_args', [ ]);
+ }
+
+ length = %ToLength(args.length);
+
+ // We can handle any number of apply arguments if the stack is
+ // big enough, but sanity check the value to avoid overflow when
+ // multiplying with pointer size.
+ if (length > 0x800000) {
+ throw %MakeRangeError('stack_overflow', []);
+ }
+
+ // Return the length which is the number of arguments to copy to the
+ // stack. It is guaranteed to be a small integer at this point.
+ return length;
+}
+
+
function STACK_OVERFLOW(length) {
throw %MakeRangeError('stack_overflow', []);
}
« no previous file with comments | « src/messages.js ('k') | src/x64/builtins-x64.cc » ('j') | test/mjsunit/harmony/reflect-apply.js » ('J')

Powered by Google App Engine