rubyjs / therubyracer

Embed the V8 Javascript Interpreter into Ruby
1.66k stars 191 forks source link

Add Template Hiearchy #366

Closed cowboyd closed 9 years ago

cowboyd commented 9 years ago

This forms the basis for ObjectTemplate and FunctionTemplate while at the same time fixing a bug in the Function callback where data was not getting passed

georgyangelov commented 9 years ago

I was just about to submit a PR for the data bug :smile:

cowboyd commented 9 years ago

heh, yeah. I was seriously going bug-eyed over that :/

cowboyd commented 9 years ago

btw, if you want to communicate in real time, I'm hanging out in https://gitter.im/cowboyd/therubyracer

georgyangelov commented 9 years ago

I'm not sure I understand what the templates are for... What is the difference with Function?

cowboyd commented 9 years ago

Great question, and my knowledge of them is dated three years ago, so please take everything I'm about to say with a grain of salt.

They're like a factory for churning out pre-configured object instances. The objects have their prototype, constructor properties configured automatically depending on how you have them set up. This mainly helped in the old version of the ruby racer when it came to representing Ruby inheritance hierarchies inside JavaScript. So each Ruby class got its own FunctionTemplate, and then we used the Inherit to make the JavaScript prototype chain lined up with the Ruby class hierarchy.

We might be able to get away with it using just Object and Function, but for some reason I remember it was hard to do.

georgyangelov commented 9 years ago

Thank you for the explanation! What baffles me now is "There can only be one function created from a FunctionTemplate in a context." -- from the FunctionTemplate docs. Why is a template useful if you can only have one function from it? Is it used for the same function across different contexts?

cowboyd commented 9 years ago

Yes, I think you use it to create one function in each context that you will be executing it in. That way, you can keep the meta-configuration for a function, i.e. where it fits in the prototype / constructor chain, static, but get a unique instance for each context.