From: | Louis Paul Santillan <lsantil@calstatela.edu> |
Newsgroups: | comp.compilers |
Date: | 6 Oct 2003 21:27:09 -0400 |
Organization: | CSUnet |
References: | 03-10-004 |
Keywords: | code, design |
Posted-Date: | 06 Oct 2003 21:27:09 EDT |
You can investigate how JavaScript implements this
<http://www.mozilla.org/js/> in C and Java. Both implementations use
stack-based, byte code interpreters/VMs and use an object property
lookup byte code to find what property is actually being requested. I
pasted examples below.
Louis
---------test.js-------------
function test( x )
{
if( x > 0 )
{
this.x = method1;
}
else
{
this.x = method2;
}
}
---------test.js-------------
--------execution------------
js> load("test.js");
js> dissrc( test )
;------------------------- 3: if( x > 0 )
00000: 3 getarg 0
00003: 3 zero
00004: 3 gt
00005: 3 ifeq 19 (14)
;------------------------- 4: {
;------------------------- 5: this.x = method1;
00008: 5 this
00009: 5 name "method1"
00012: 5 setprop "x"
00015: 5 pop
00016: 5 goto 27 (11)
;------------------------- 6: }
;------------------------- 7: else
;------------------------- 8: {
;------------------------- 9: this.x = method2;
00019: 9 this
00020: 9 name "method2"
00023: 9 setprop "x"
00026: 9 pop
js> quit()
--------execution------------
On 4 Oct 2003, Gabriele Farina wrote:
> Hi guys,
[SNIP]
> test = class()
> input value
>
> if value == 10
> test.a_new_method = function(a) print a
> else
> test.a_new_method = function(a) print 'Your wrong the number, you
> inserted'.append(a)
>
> instance = new test()
> instance.a_new_method(value)
>
[SNIP]
> bye
> [The only way I know to handle anonymous functions is to give each on a
> secret private name at compile time. As far as adding new methods at
> runtime, it's doable but it means that you have to keep most of the
> compiler symbol table at runtime and possibly the whole compiler if
> you're adding new code on the fly. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.