Stack variable substitution in JS code using V8

16 views
Skip to first unread message

Abhishek Singh

unread,
Jun 30, 2016, 11:16:27 PM6/30/16
to v8-u...@googlegroups.com
Hi,

As part of v8 embedding work recently, I've encountered one more use case:

Sample app.js:
===========

var database_table = "beer_sample";
var city = "BLR";

// "query" is a FunctionTemplate exposed via an ObjectTemplate
var queryResult = query("select * from $database_table where $database_table.city= $city");

// Assuming "city" is top level json field in documents within that DB table, sample doc could look like:
// {"beer_count": 1000, city: "BLR", "brewery_location": "business_district"}

===========

Within the C++ Handler FunctionTemplate of "query" call needs to poke at what was the state of stack variables in JS and after replacing it, it would require to fire query. Just to clarify, query that Handler would require to fire:

"select * from beer-sample where beer-sample.city= BLR"

Please suggest if there is a way to do this currently using v8, else I suppose I would require to convert JS source to AST and after replacing variables appropriately I would need to convert AST to valid JS source.

 
--
Thanks,

Abhishek Singh

Ben Noordhuis

unread,
Jul 1, 2016, 5:53:27 AM7/1/16
to v8-users
You could inspect the stack frame using the debug API (consult
src/debug/debug.js and src/debug/mirrors.js) but it's arguably a bit
daft and certainly inefficient. A more idiomatic and portable JS
solution would be tagged template literals[0].

[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Abhishek Singh

unread,
Jul 1, 2016, 6:58:54 AM7/1/16
to v8-u...@googlegroups.com
Thanks Ben, tagged template literals would solve my problem is a portable manner.

-- 
-- 
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages