#include <string.h>
+static njs_function_t *njs_function_copy(njs_vm_t *vm,
+ const njs_function_t *function);
static njs_native_frame_t *njs_function_frame_alloc(njs_vm_t *vm, size_t size);
static njs_ret_t njs_normalize_args(njs_vm_t *vm, njs_value_t *args,
uint8_t *args_types, nxt_uint_t nargs);
njs_function_t *
njs_function_value_copy(njs_vm_t *vm, njs_value_t *value)
{
- size_t size;
- nxt_uint_t n, nesting;
njs_function_t *function, *copy;
function = value->data.u.function;
return function;
}
+ copy = njs_function_copy(vm, function);
+ if (nxt_slow_path(copy == NULL)) {
+ njs_memory_error(vm);
+ return NULL;
+ }
+
+ value->data.u.function = copy;
+
+ return copy;
+}
+
+
+static njs_function_t *
+njs_function_copy(njs_vm_t *vm, const njs_function_t *function)
+{
+ size_t size;
+ nxt_uint_t n, nesting;
+ njs_function_t *copy;
+
nesting = (function->native) ? 0 : function->u.lambda->nesting;
size = sizeof(njs_function_t) + nesting * sizeof(njs_closure_t *);
return NULL;
}
- value->data.u.function = copy;
-
*copy = *function;
copy->object.__proto__ = &vm->prototypes[NJS_PROTOTYPE_FUNCTION].object;
copy->object.shared = 0;
return NXT_ERROR;
}
- function = nxt_mp_alloc(vm->mem_pool, sizeof(njs_function_t));
+ function = njs_function_copy(vm, args[0].data.u.function);
if (nxt_slow_path(function == NULL)) {
njs_memory_error(vm);
return NXT_ERROR;
}
- *function = *args[0].data.u.function;
-
- function->object.__proto__ = &vm->prototypes[NJS_PROTOTYPE_FUNCTION].object;
- function->object.shared = 0;
- function->object.extensible = 1;
-
if (nargs == 1) {
args = (njs_value_t *) &njs_value_undefined;
"var b = f.bind('1', '2', '3'); b.apply()"),
nxt_string("123") },
+ { nxt_string("function f() { var a; return (function() { a = 1; return a; }).bind()() } f()"),
+ nxt_string("1") },
+
+ { nxt_string("function f() { var a; function baz() { a = 1; return a; } return baz.bind()(); } f()"),
+ nxt_string("1") },
+
+ { nxt_string("function f(a, b) { return a + b }"
+ "f(3,4) === f.bind()(3,4)"),
+ nxt_string("true") },
+
{ nxt_string("var obj = {prop:'abc'}; "
"var func = function(x) { "
" return this === obj && x === 1 && arguments[0] === 1 "