Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/v8natives.js

Issue 3046010: Implement Function.prototype.bind (ES5 15.3.4.5).... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 function DefineOwnProperty(obj, p, desc, should_throw) { 532 function DefineOwnProperty(obj, p, desc, should_throw) {
533 var current = GetOwnProperty(obj, p); 533 var current = GetOwnProperty(obj, p);
534 var extensible = %IsExtensible(ToObject(obj)); 534 var extensible = %IsExtensible(ToObject(obj));
535 535
536 // Error handling according to spec. 536 // Error handling according to spec.
537 // Step 3 537 // Step 3
538 if (IS_UNDEFINED(current) && !extensible) 538 if (IS_UNDEFINED(current) && !extensible)
539 throw MakeTypeError("define_disallowed", ["defineProperty"]); 539 throw MakeTypeError("define_disallowed", ["defineProperty"]);
540 540
541 if (!IS_UNDEFINED(current) && !current.isConfigurable()) { 541 if (!IS_UNDEFINED(current) && !current.isConfigurable()) {
542 // Step 5 and 6 542 // Step 5 and 6
543 if ((!desc.hasEnumerable() || 543 if ((!desc.hasEnumerable() ||
544 SameValue(desc.isEnumerable() && current.isEnumerable())) && 544 SameValue(desc.isEnumerable() && current.isEnumerable())) &&
545 (!desc.hasConfigurable() || 545 (!desc.hasConfigurable() ||
546 SameValue(desc.isConfigurable(), current.isConfigurable())) && 546 SameValue(desc.isConfigurable(), current.isConfigurable())) &&
547 (!desc.hasWritable() || 547 (!desc.hasWritable() ||
548 SameValue(desc.isWritable(), current.isWritable())) && 548 SameValue(desc.isWritable(), current.isWritable())) &&
549 (!desc.hasValue() || 549 (!desc.hasValue() ||
550 SameValue(desc.getValue(), current.getValue())) && 550 SameValue(desc.getValue(), current.getValue())) &&
551 (!desc.hasGetter() || 551 (!desc.hasGetter() ||
552 SameValue(desc.getGet(), current.getGet())) && 552 SameValue(desc.getGet(), current.getGet())) &&
553 (!desc.hasSetter() || 553 (!desc.hasSetter() ||
554 SameValue(desc.getSet(), current.getSet()))) { 554 SameValue(desc.getSet(), current.getSet()))) {
555 return true; 555 return true;
556 } 556 }
557 557
558 // Step 7 558 // Step 7
559 if (desc.isConfigurable() || desc.isEnumerable() != current.isEnumerable()) 559 if (desc.isConfigurable() || desc.isEnumerable() != current.isEnumerable())
560 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); 560 throw MakeTypeError("redefine_disallowed", ["defineProperty"]);
561 // Step 9 561 // Step 9
562 if (IsDataDescriptor(current) != IsDataDescriptor(desc)) 562 if (IsDataDescriptor(current) != IsDataDescriptor(desc))
563 throw MakeTypeError("redefine_disallowed", ["defineProperty"]); 563 throw MakeTypeError("redefine_disallowed", ["defineProperty"]);
564 // Step 10 564 // Step 10
565 if (IsDataDescriptor(current) && IsDataDescriptor(desc)) { 565 if (IsDataDescriptor(current) && IsDataDescriptor(desc)) {
566 if (!current.isWritable() && desc.isWritable()) 566 if (!current.isWritable() && desc.isWritable())
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after
1092 var name = %FunctionGetName(func); 1092 var name = %FunctionGetName(func);
1093 return 'function ' + name + source; 1093 return 'function ' + name + source;
1094 } 1094 }
1095 1095
1096 1096
1097 function FunctionToString() { 1097 function FunctionToString() {
1098 return FunctionSourceString(this); 1098 return FunctionSourceString(this);
1099 } 1099 }
1100 1100
1101 1101
1102 // ES5 15.3.4.5
1103 function FunctionBind(this_arg) { // Length is 1.
1104 if (!IS_FUNCTION(this)) {
1105 throw new $TypeError('Bind must be called on a function');
1106 }
1107 // this_arg is not an argument that should be bound.
1108 var argc_bound = %_ArgumentsLength() - 1;
1109 if (argc_bound > 0) {
1110 var bound_args = new $Array(argc_bound);
1111 for(var i = 0; i < argc_bound; i++) {
1112 bound_args[i] = %_Arguments(i+1);
1113 }
1114 }
1115 global.print(argc_bound);
Aaron.Heckmann_gmail.com 2010/07/27 02:21:47 Looks like this line should be removed?
1116 var fn = this;
1117 var result = function() {
1118 // Combine the args we got from the bind call with the args
1119 // given as argument to the invocation.
1120 var argc = %_ArgumentsLength();
1121 var args = new $Array(argc + argc_bound);
1122 // Add bound arguments.
1123 for (var i = 0; i < argc_bound; i++) {
1124 args[i] = bound_args[i];
1125 }
1126 // Add arguments from call.
1127 for (var i = 0; i < argc; i++) {
1128 args[argc_bound + i] = %_Arguments(i);
1129 }
1130 // If this is a construct call we use a special runtime method
1131 // to generate the actual object using the bound function.
1132 if (%_IsConstructCall()) {
1133 return %NewObjectFromBound(fn, args);
1134 }
1135 return fn.apply(this_arg, args);
1136 };
1137
1138 // We already have caller and arguments properties on functions,
1139 // which are non-configurable. It therefore makes no sence to
1140 // try to redefine these as defined by the spec. The spec says
1141 // that bind should make these throw a TypeError if get or set
1142 // is called and make them non-enumerable and non-configurable.
1143 // To be consistent with our normal functions we leave this as it is.
1144
1145 // Set the correct length.
1146 var length = (this.length - argc_bound) > 0 ? this.length - argc_bound : 0;
1147 %FunctionSetLength(result, length);
1148
1149 return result;
1150 }
1151
1152
1102 function NewFunction(arg1) { // length == 1 1153 function NewFunction(arg1) { // length == 1
1103 var n = %_ArgumentsLength(); 1154 var n = %_ArgumentsLength();
1104 var p = ''; 1155 var p = '';
1105 if (n > 1) { 1156 if (n > 1) {
1106 p = new $Array(n - 1); 1157 p = new $Array(n - 1);
1107 // Explicitly convert all parameters to strings. 1158 // Explicitly convert all parameters to strings.
1108 // Array.prototype.join replaces null with empty strings which is 1159 // Array.prototype.join replaces null with empty strings which is
1109 // not appropriate. 1160 // not appropriate.
1110 for (var i = 0; i < n - 1; i++) p[i] = ToString(%_Arguments(i)); 1161 for (var i = 0; i < n - 1; i++) p[i] = ToString(%_Arguments(i));
1111 p = p.join(','); 1162 p = p.join(',');
(...skipping 11 matching lines...) Expand all
1123 %FunctionSetName(f, "anonymous"); 1174 %FunctionSetName(f, "anonymous");
1124 return %SetNewFunctionAttributes(f); 1175 return %SetNewFunctionAttributes(f);
1125 } 1176 }
1126 1177
1127 %SetCode($Function, NewFunction); 1178 %SetCode($Function, NewFunction);
1128 1179
1129 // ---------------------------------------------------------------------------- 1180 // ----------------------------------------------------------------------------
1130 1181
1131 function SetupFunction() { 1182 function SetupFunction() {
1132 InstallFunctions($Function.prototype, DONT_ENUM, $Array( 1183 InstallFunctions($Function.prototype, DONT_ENUM, $Array(
1184 "bind", FunctionBind,
1133 "toString", FunctionToString 1185 "toString", FunctionToString
1134 )); 1186 ));
1135 } 1187 }
1136 1188
1137 SetupFunction(); 1189 SetupFunction();
OLDNEW
« no previous file with comments | « src/runtime.cc ('k') | test/mjsunit/function-bind.js » ('j') | test/mjsunit/function-bind.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698