12

I saw % in many codes. Can you explain to me its purpose or what it does?

Edit: I know the operand in math 13 % 10 = 3 but what I saw is like return %foo.

Encountered this while looking through v8 source files

Why are some of the function invocations preceded by a % sign?

%CheckIsBootstrapping();
//---
// Only used by async-await.js
function RejectPromise(promise, reason, debugEvent) {
  %PromiseReject(promise, reason, debugEvent);
}
//---
var callbacks = %create_resolving_functions(promise, debugEvent);
rahulroy9202
  • 2,730
  • 3
  • 32
  • 45
user1365010
  • 3,185
  • 8
  • 24
  • 43

1 Answers1

12

Based on the link you provided in the comments, the % character appears to be used in some of the V8 JavaScript engine source code to indicate a method of the C++ runtime that will be executed while parsing the JavaScript source.

For example, the line in string.js:

return %StringBuilderConcat(parts, len + 1, "");

When encountered by the parser, the StringBuilderConcat method will be executed. You can find a list of the runtime methods available to the V8 JavaScript files in runtime.h (note, I have no experience with C++, so for all I know this has nothing to do with the StringBuilderConcat method referenced in string.js, but I think it's the same thing):

#define RUNTIME_FUNCTION_LIST_ALWAYS_1(F) \
  /* Property access */ \
  F(GetProperty, 2, 1) \
  F(KeyedGetProperty, 2, 1) \
  /* ... */
  F(StringBuilderConcat, 3, 1) \
  /* ... */

As has already been stated, return %foo would throw a SyntaxError in JavaScript.

LukeG
  • 117
  • 8
James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • 2
    Minor correction: parser does not really _execute_ any code itself (with exception of `%IS_VAR` which cause parsing error if argument is not a variable). Parser just creates CallRuntime AST node (https://github.com/v8/v8/blob/master/src/parser.cc#L4720-4763). Compiler(s) then either emit call to the corresponding runtime function (https://github.com/v8/v8/blob/master/src/ia32/full-codegen-ia32.cc#L3797-3834) or inline it if it is inlinable (example `%_IsSmi`: https://github.com/v8/v8/blob/master/src/ia32/full-codegen-ia32.cc#L2518-2536). – Vyacheslav Egorov Jun 26 '12 at 08:58