3

I have a compiler for a programming language that targets JavaScript. That language needs some global functions such as: alloc, memcpy, write and so on. My question is: where should I position those functions? I have pretty much 2 options:

1: inside a self-contained object, runtime. Compiled functions would need to receive that object:

function example_of_compiled_function(runtime, arg0, arg1){
    var arrayPtr = runtime.alloc(arg0);
    for (var i=0; i<arg0; ++i)
        runtime.write(arrayPtr+i, arg1);
    return arrayPtr;
};

2: globally. Compiled functions wouldn't need to receive an additional object:

function example_of_compiled_function(arg0, arg1){
    var arrayPtr = alloc(arg0);
    for (var i=0; i<arg0; ++i)
        write(arrayPtr+i, arg1);
    return arrayPtr;
}

Both ways have problems. For (1), there is a considerable slowdown for accessing methods inside an object, and using compiled functions becomes more complicated since users have to manually create that object and feed it to them. For (2), it is faster and simpler, but might pollute the global namespace. Is there a satisfactory solution for this?

  • 1
    How considerable is the slowdown (sorry, I really don't know)? Since that's running client-side, I think you'd have to push really hard before the user noticed the difference. JQuery and JQuery plug-ins use "$.abc", so I doubt it's that much worse ... recalling an old conversation ... if you don't want to deeply nest your namespace, break it up with "_" instead of nesting objects with "." e.g. $.mine_group_things instead of $.mine.group.things – Rob Dec 31 '14 at 1:17
  • It is a considerable slowdown when you have things such as "runtime.write(a,b)" inside a inner loop that runs a million times. JQuery accessors are never used in such inner loops, but for a compiled code, that would happen anytime you have a nested loop. – MaiaVictor Dec 31 '14 at 1:44
  • can you use var w = runtime.write before your big loop starts, and then just w(a,b)? I believe the part that's slow is deferencing the function inside the object ... once it's running, it doesn't know that it's inside something else – Rob Dec 31 '14 at 1:55
  • Yes I guess that can be done. – MaiaVictor Dec 31 '14 at 2:30
  • 5
    What you are looking for is simply called "namespace". And if you just google for "javascript namespaces", you will find plenty of articles how to "emulate" namespaces in Javascript correctly. Don't worry so much about performance, you are falling into the "premature optization" trap. As RobY has correctly pointed out, the user of your runtime can solve this easily for the typically small parts of his code where performance might get a problem. – Doc Brown Dec 31 '14 at 8:01
1

How about putting runtime in a closure?

function mycode(runtime) {    
   function example_of_compiled_function(arg0, arg1){
    var arrayPtr = runtime.alloc(arg0);
    for (var i=0; i<arg0; ++i)
        runtime.write(arrayPtr+i, arg1);
    return arrayPtr;
   }
};

Simply pass in runtime to this function (or any further generated functions), and it'll be accessible to all code in the closure without explicitly passing it everywhere.

  • The problem is that functions can be compiled during the program execution. In this case it wouldn't work. I'm sorry, I didn't predict your answer! – MaiaVictor Dec 31 '14 at 1:43
  • @Dokkat, how does that stop you? If you are generating functions at runtime, can't you still generate the closure at runtime? – Winston Ewert Dec 31 '14 at 1:53
  • If I get the idea correctly, as the "runtime" is hidden inside a closure, then the functions generated after the first one will not be able to access the same "runtime". That is, if I call "eval" on your code twice, it creates two runtimes. – MaiaVictor Dec 31 '14 at 2:21
  • @Dokkat, see edit. – Winston Ewert Dec 31 '14 at 2:47
  • Hmm, fair enough. I guess that is a good approach. I might ask the question again in C, though, since there is a C backend and closures aren't available there. – MaiaVictor Dec 31 '14 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.