Dynamic methods

13 views
Skip to first unread message

Philipp Pirozhkov

unread,
Jun 18, 2012, 7:36:51 AM6/18/12
to javascript-...@googlegroups.com
Hi,

I've stuck with the need to allow JS call arbitrary methods on an object:

class A
  def method_missing method, *args, &block
    puts "method #{method} called"
  end
end

cxt = V8::Context.new
cxt['a'] = A.new
cxt.eval("a.some_method()") # V8::JSError: Object #<rb::A> has no method 'some_method'

Do you think that it's a security flaw to allow the script call any method on an object?
Probably we should allow this if the object has metod_missing defined and only throw JSError if method_missing throws something?

BR, Phil

Charles Lowell

unread,
Jun 18, 2012, 10:19:27 AM6/18/12
to javascript-...@googlegroups.com, javascript-...@googlegroups.com
[]() is the js equivalent of method_missing. Return a proc, lambda or method object from that.

Sent from my iPhone

Charles Lowell

unread,
Jun 18, 2012, 10:38:17 AM6/18/12
to javascript-...@googlegroups.com, javascript-...@googlegroups.com
Also remember to yield unless you explicitly want to return nil for a dynamic property. This will allow property resolution to proceed up the prototype chain.

Sent from my iPhone

On Jun 18, 2012, at 13:36, Philipp Pirozhkov <pirj...@gmail.com> wrote:

Philipp Pirozhkov

unread,
Jun 19, 2012, 3:45:21 PM6/19/12
to javascript-...@googlegroups.com
Thanks! Works like a charm. Shame on me I haven't figured it out myself.


Also remember to yield unless you explicitly want to return nil for a dynamic property. This will allow property resolution to proceed up the prototype chain.

You mean calling 'super' instead of returning nil? Yep, sure

Charles Lowell

unread,
Jun 19, 2012, 4:17:54 PM6/19/12
to javascript-...@googlegroups.com, therub...@googlegroups.com
Actually, I mean `yield`. That way it works whether you have a superclass/module with []() defined or not. Remember, we want to go up the JavaScript prototype chain, not the Ruby ancestor chain.



Charles Lowell 
thefrontside.net | twitter: @cowboyd | github: cowboyd




Reply all
Reply to author
Forward
0 new messages