Looking through the promise code in v8 source code, I noticed a lot of functions that starts with %, what are those? do they have any special meaning?
example here https://chromium.googlesource.com/v8/v8/+/3.29.45/src/promise.js?autodive=0%2F
like %DebugPushPromise
They're internal built-in runtime functions. More info here: https://v8.dev/docs/builtin-functions
Related
I am inspecting some code on Github and I need to quickly understand if the script is javascript or typescript.
Are there any easy shortcuts or clues to this?
As an example, in this image from https://www.typescriptlang.org/ gives me a clue that if an array is declared with a bracket [] after the variable name, then it is typescript.
You can distinguish between the two formats by looking simply at the functions.
Are any of the functions specifying types such as 'string,integer,object,array,function?...' etc?
In traditionally javascript this is not allowed.
Best,
AT
Also as others may have pointed out, you can also check the file extension
In Javascript for some methods, I don't see the suggestions. For example indexOf, charCodeAt and so on. Is it possible to activate suggestions for these built-in Javascript keywords?
I think you should leverage VS Code ability to work with JSDoc.
As you can see without JSDoc, VS Code can't infer that bar is a string. After documenting the parameter properly, it is able to make some meaningful suggestions:
Browsing the v8 tree, under the src directory, some js files were there, providing some basic JS objects like Math, Array etc. Browsing those files, I saw identifiers including a percent sign (%) in their names, i.e. %Foo. I first naively thought it was some other allowed character in JS's identifiers, but when I tried it in shell, it yelled at me, saying that I'm violating syntax rules. But if it is a syntax error, how come d8 works? Here are an example from the actual source code:
src/apinatives.js lines 44 to 47, git clone from github/v8/v8
function Instantiate(data, name) {
if (!%IsTemplate(data)) return data;
var tag = %GetTemplateField(data, kApiTagOffset);
switch (tag) {
src/apinatives.js lines 41 to 43, git clone from github/v8/v8
function SetConstructor() {
if (%_IsConstructCall()) {
%SetInitialize(this);
How come this identifiers do not yield syntax errors. All js files, including math.js and string.js and all others?:wq
It is not technically valid JavaScript. These are calls to V8 runtime functions. From that page:
Much of the JavaScript library is implemented in JavaScript code
itself, using a minimal set of C++ runtime functions callable from
JavaScript. Some of these are called using names that start with %,
and using the flag "--allow-natives-syntax". Others are only called by
code generated by the code generators, and are not visible in JS, even
using the % syntax.
If you look in parser.cc you can see some code relating to allow_natives_syntax that determines whether the parser will accept this extension to the JavaScript language that V8 is using to interact with its runtime. These files must be parsed with that option enabled.
I would speculate that V8 does not allow you to make these calls by default both because it would contradict the JavaScript standard and because it would probably allow you to do things to the runtime you should not be able to do.
I am trying to use Rhino to covert some JavaScript code to Java. As anyone who has any experience at all with JavaScript knows "document" is used for lots of things. So, it is used in various functions in the code I am using. However, when I execute Rhino it just gives me this error js: uncaught JavaScript runtime exception: ReferenceError: "document" is not defined.
Does anyone know what is causing this? And if so how to fix it? I am using Rhino version 1.7 on Ubuntu 14.10 64 bit.
Or if anyone could atleast show me how to evaluate JavaScript from within Java. I have done some research, but the code never works.
Rhino is a just interpreter with only defines base object. Object such as 'document' 'window' is external HTML DOM object. It is out of scope for Rhino, or any other Javascript interpreter (such as jscript(IE) , V8 , spidermonkey, and nashorn).
Please refer java-based HtmlUnit which also uses Rhino and it defines most of HTML DOM objects. It is updated quite well.
After hours of digging and research I have been able to find out how to resolve this problem...
I have done two things.
I stopped using Rhino (considering it is outdated) and I switched to NashornI also found out how to implement document How to append xml nodes (as a string) into an existing XML Element node (only using java builtins)?
That took very unconventional means to find this answer, which would make any programmer go "Oh...That's one way to do it."
The way implemented in the question actually worked.
DISCLAIMER
My problems are not completely solved. Nashorn still gives me this error Exception in thread "main" java.lang.ClassCastException: Cannot cast java.lang.String to org.w3c.dom.DocumentType But, that's another question.
I'm trying to enjoy some of the awesome javascript code golf submissions on anarchy code golf, but I keep seeing things like:
for(;s=readline();)print("h"+/t.*/(s))
...which was the JS winner for: http://golf.shinh.org/p.rb?ttp
I don't understand how that is correct javascript syntax, and I even tried resubmitting that, but it said object is not a function, which is something along the lines of what I would expect to happen.
Was this some kind of glitch or shorthand or something in an older javascript version?
Was this some kind of glitch or shorthand or something in an older javascript version?
More or less, yes. According to that site's version info, it uses SpiderMonkey (Mozilla's JavaScript engine), which used to have the feature that regular-expression objects were callable; that is, that if re was a regular-expression object, then re(...) was equivalent to re.exec(...). That feature was removed in this change, a result of Bug 582717, and that site has since updated to a version that incorporates that removal.