views:

27

answers:

2

I have a bizarre need here, and I am unsure of it's feasibility. I can only think how I would do it using threads (To create another thread that performs a Quine function along side a thread running the script I want to Quine and execute at the same time (without manually adding alerts everywhere!!!), but javascript doesn't have that functionality, right?

I have some JavaScript which is interpreted by an application with minimal JavaScript debugging capability of it's own. My script is falling over and since it also uses some statements only understood by this application I cannot debug this within Firefox or Internet Explorer because they cannot handle these custom statements. What I am hoping I'll be able to achieve is a program that can mimic another program but also perform it's own functions.

For arguments sake say I have a script called hello.js that outputs "Hello World!" 100 times and when provided to the application it interprets this, however falls over at some point but I can't tell why, where and when because of the limited debug capability.

function main(){
     for(var i=0; i<100; i++){
          alert('Hello World!\n');
     }
}

I then want to have a script that I can pass to the application instead that will pretend to be the script above but will also alert before each statement is executed. I could just do this:

function main(){
     alert('main()')
     for(var i=0; i<100; i++){
          alert("alert('Hello World!\n');");
          alert('Hello World!\n');
     }
}

However I am sure you can see how for a long program this would be an arduous task. Do instead I want to make a program that can perform like this:

function main(){
     var text = loadJSScript("C:\\Script\\Hello.js"); //Loads a text/javascript file line by line into an array
     for(var i=0; i<text.length; i++){
          var statement = text[i];
          alert("Line Number: " + i + " Statement: " + statement); //Output the line number and statement before executing
          execute(statement); //A function that executes the statement from the other file (as if a separate thread)
     }
}
A: 

To my knowledge, JavaScript is purely mono-threaded.
Moreover, the loadJSScript("C:\\Script\\Hello.js"); statement is fishy, since you mention browsers. JS in browsers (in Web pages, at least) cannot access the filesystem (beside, that's not portable!).
You can perhaps do something like that in Microsoft's JScript (in WSH), though.

PhiLho
It's a browser residing in a client-side application with file system capability using some proprietary javascript functions. Thanks for replying though :)
Emdiesse
+1  A: 

This really depends on your target audience (browser use).

You can get a very effective form of pseudo-threading by using web workers.

http://ejohn.org/blog/web-workers/

However this is not widely supported. You could use this where available.

You can also get some behaviour similar to threading by using setTimeout to execute the code.

You can see how timers are queued here: http://ejohn.org/blog/how-javascript-timers-work/

You could also do the Quine calculation server side, and call using AJAX. This does give an asynchronous request.

These last 2 options aren't threading, but the code will not wait for a response before continuing.

Matt
http://articles.sitepoint.com/article/multi-threading-javascriptAn example of an article on Multi-Threading Javascript, exploring the use o setInterval (similar to setTimeout but recurring)But if possible AJAX seems neater solution.
João
Cheers I'll have a look into this and try out web workers.
Emdiesse