MCPcopy Create free account
hub / github.com/Snapchat/Valdi / nameFromJSFunction

Function nameFromJSFunction

valdi/src/valdi/runtime/JavaScript/JavaScriptUtils.cpp:354–383  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

352}
353
354StringBox nameFromJSFunction(IJavaScriptContext& jsContext, const JSValue& jsValue) {
355 static auto kNameKey = STRING_LITERAL("name");
356 static auto kAnonymousFunction = STRING_LITERAL("<anon>");
357 static auto kAnonymousBoundFunction = STRING_LITERAL("bound <anon>");
358 static auto kNameWhenBoundAnonymous = STRING_LITERAL("bound ");
359
360 JSExceptionTracker exceptionTracker(jsContext);
361 auto name = jsContext.getObjectProperty(jsValue, jsContext.getPropertyNameCached(kNameKey), exceptionTracker);
362 if (!exceptionTracker || jsContext.isValueUndefined(name.get())) {
363 exceptionTracker.clearError();
364 return kAnonymousFunction;
365 }
366
367 auto str = jsContext.valueToString(name.get(), exceptionTracker);
368 if (!exceptionTracker) {
369 exceptionTracker.clearError();
370 return kAnonymousFunction;
371 }
372
373 if (str.isEmpty()) {
374 // In JS empty string means name no set
375 return kAnonymousFunction;
376 }
377
378 if (str == kNameWhenBoundAnonymous) {
379 return kAnonymousBoundFunction;
380 }
381
382 return str;
383}
384
385static bool checkRefNotCircular(const JSValue& value,
386 JavaScriptCircularRefChecker<JSValue>& circularRefChecker,

Calls 7

getPropertyNameCachedMethod · 0.80
clearErrorMethod · 0.80
getMethod · 0.65
valueToStringMethod · 0.65
getObjectPropertyMethod · 0.45
isValueUndefinedMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected