From 8ad67de59ec2fa838417b6007cf43bcff7dcb778 Mon Sep 17 00:00:00 2001 From: Gabriel Nunes Date: Tue, 25 Feb 2020 12:59:56 -0800 Subject: [PATCH] Update documentation for Function::call to reflect how 'this' behaves in non-strict mode Summary: The `Function::call` method says it leaves the JS `this` object undefined. According to my tests, that's not completely true: if the function is defined to use strict mode via `"use strict"` either inside itself or in the file it was defined in, it does leave it `undefined`, but if the function is defined in non-strict mode, it sets `this` to the global object instead. This diff updates the documentation to reflect this. Reviewed By: mhorowitz Differential Revision: D19613483 fbshipit-source-id: 4b9ecf81c6318592be05a216748dcb22e32989f8 --- ReactCommon/jsi/jsi/jsi.h | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/ReactCommon/jsi/jsi/jsi.h b/ReactCommon/jsi/jsi/jsi.h index 2309cdaf319..94120db2781 100644 --- a/ReactCommon/jsi/jsi/jsi.h +++ b/ReactCommon/jsi/jsi/jsi.h @@ -811,18 +811,30 @@ class Function : public Object { unsigned int paramCount, jsi::HostFunctionType func); - /// Calls the function with \c count \c args. The \c this value of - /// the JS function will be undefined. + /// Calls the function with \c count \c args. The \c this value of the JS + /// function will not be set by the C++ caller, similar to calling + /// Function.prototype.apply(undefined, args) in JS. + /// \b Note: as with Function.prototype.apply, \c this may not always be + /// \c undefined in the function itself. If the function is non-strict, + /// \c this will be set to the global object. Value call(Runtime& runtime, const Value* args, size_t count) const; /// Calls the function with a \c std::initializer_list of Value - /// arguments. The \c this value of the JS function will be - /// undefined. + /// arguments. The \c this value of the JS function will not be set by the + /// C++ caller, similar to calling Function.prototype.apply(undefined, args) + /// in JS. + /// \b Note: as with Function.prototype.apply, \c this may not always be + /// \c undefined in the function itself. If the function is non-strict, + /// \c this will be set to the global object. Value call(Runtime& runtime, std::initializer_list args) const; /// Calls the function with any number of arguments similarly to - /// Object::setProperty(). The \c this value of the JS function - /// will be undefined. + /// Object::setProperty(). The \c this value of the JS function will not be + /// set by the C++ caller, similar to calling + /// Function.prototype.call(undefined, ...args) in JS. + /// \b Note: as with Function.prototype.call, \c this may not always be + /// \c undefined in the function itself. If the function is non-strict, + /// \c this will be set to the global object. template Value call(Runtime& runtime, Args&&... args) const;