From dafa5ab774446d600ce6232c10703c295ed1a875 Mon Sep 17 00:00:00 2001 From: wraitii Date: Tue, 20 Jun 2023 11:40:18 +0000 Subject: [PATCH] Minor optimisations when calling JS functions from C++ ScriptFunction::Call_ checks HasProperty, but then JS_CallFunctionName calls GetProperty. This is redundant and can be simplified. Reviewed By: phosit Differential Revision: https://code.wildfiregames.com/D5000 This was SVN commit r27726. --- source/scriptinterface/FunctionWrapper.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/source/scriptinterface/FunctionWrapper.h b/source/scriptinterface/FunctionWrapper.h index 978c124709..dc0b2b8e83 100644 --- a/source/scriptinterface/FunctionWrapper.h +++ b/source/scriptinterface/FunctionWrapper.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -234,10 +234,9 @@ private: if (!JS_ValueToObject(rq.cx, val, &obj) || !obj) return false; - // Check that the named function actually exists, to avoid ugly JS error reports - // when calling an undefined value - bool found; - if (!JS_HasProperty(rq.cx, obj, name, &found) || !found) + // Fetch the property explicitly - this avoids converting the arguments if it doesn't exist. + JS::RootedValue func(rq.cx); + if (!JS_GetProperty(rq.cx, obj, name, &func) || func.isUndefined()) return false; JS::RootedValueVector argv(rq.cx); @@ -246,11 +245,11 @@ private: bool success; if constexpr (std::is_same_v) - success = JS_CallFunctionName(rq.cx, obj, name, argv, ret); + success = JS_CallFunctionValue(rq.cx, obj, func, argv, ret); else { JS::RootedValue jsRet(rq.cx); - success = JS_CallFunctionName(rq.cx, obj, name, argv, &jsRet); + success = JS_CallFunctionValue(rq.cx, obj, func, argv, &jsRet); if constexpr (!std::is_same_v) { if (success)