简体   繁体   中英

What is the best way to compile JavaScript-like structures to static, fast C++?

On the development of a compiler from a language very similar to JavaScript to C++, I need a way to represent data structures. JavaScript's main data structures are Arrays and Hash-Tables. Arrays are more straighforward: I can use a vector of untyped pointers . It needs to be a vector because JS arrays are dynamic, and of pointers because JS arrays can hold any kind of object, for example:

var array = [1,2,[3,4],"test"];

I can't see a way to represent this other than that (is there?). For the hashes, I could use something similar, except including the string hashing step on access.

The problem is: JavaScript hashes are JIT-compiled into actual C++ objects which probably are much faster than hashes . This way, I'm afraid my attempt to generate C++ like that will actually result in slower code than the JavaScript version!

  1. Does that make sense?
  2. What would be the best approach to my compiler?

If this is an AOT compiler you can only process the hash keys that you see at compile-time, obviously. In this case you can change hash accesses to known keys to array accesses, giving each known key a small integer as index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM