P
pedz
I need either a
1) new language
2) new JS library
3) both 1 and 2
4) new way of thinking
Given:
I thought I'd be brave and ask here rather than there.
More and more in browsers the programming model is becoming event
driven. Take a typical Ajax call. My personal thought process is
something like:
1) Send the Ajax request
2) wait for it to get back
3) dig out the piece I need
4) send the next Ajax request
5) wait for it to complete
6) etc.
The problem is that steps 2 and 5 are done by setting up call back
handlers and so the result is that I do not get to "see" the list of
steps.
In today's particular adventure, I'm not doing Ajax requests but
working with Web Workers which have the same event driven
characteristic. I could maybe change my thinking to not mind this at
all but then I tried hooking up JSpec as a testing framework and it
all fell apart. JSpec at its root is very sequenctial. And I'm going
to bump into this same problem almost everywhere I go.
This got me thinking about a JS library which would create a thing. I
don't want to call it a "thread" nor a "context" because both of those
terms have so much meaning. So, for now, I'm going to call it a VSF
for Virtual Stack Frame.
To start, the VSF has a "pc" and a list of "instructions". The pc
starts at 0 and works forward. The instructions are calls to
Javascript ... here is my first weakness ... "functions" ? (I hope
thats right.)
What little I've used JS (most of it via Prototype (blush)), I really
like the bind and bindAsEventHandler so that I can get the "this" set
like I want. So, I was going to allow the user to specify the value
of "this" as well as a list of arguments to pass to the function.
Each of these functions needs a way to get back to the VSF so it would
either be passed as an argument or somehow accessable via the context.
JSpec uses the "with" statement for this but I know that Crockford, in
particular, dislikes the with statement.
The details of how the interface to the functions works has a lot of
options but in general, the idea is that a function can tell the VSF
to not proceed to the next instruction. Instead the VSF would just
exit
(return). For example, each instruction could return true if the next
instruction should be executed and false if not. I suppose it could
be
part of the instruction's specification.
Lets say that an instruction starts an Ajax call. It would set its
completion handler to call vsf.resume where vsf is an instance of a
VSF which initiated the Ajax call. The instruction itself would
return false or do whatever is needed to cause the VSF to not go to
the next instruction.
The call to vsf.resume picks up at the next instruction and continues.
Added entry points to VFS would be to get and set "instance" variables
(as Ruby calls them) and also to chain VSF's so that a stack of VSF's
can be created.
At this point, there is enough power to be dangerous. The user
creates his VSF instances and specifies the list of instructions.
Calls vsf.resume and off it goes.
More power could be added by adding features like being able to muck
with the PC, being able to "return" from the VSF. The other idea was
to implant a list of objects with the connotation that the list of
instructions would be executed once with each object. The
possibilities expand rather quickly which is what brought up point #1
way up at the start of this.
Perhaps, as a second evolution to this, a DSL could be wrapped around
it.
Also, I've been thinking in "single threaded" terms but a VSF could
have a parent concept perhaps called a VPS (Virtual Program Sequence)
which is just a list of instructions. Then a VSF would be started
from a VPS. The idea is that multiple instances of the same VPS could
be running around making a mess of things all at the same time. This
would introduce the need of locking, etc.
But, you ask, what about JSpec? Well, it would need to be redone to
use VSFs but I think the majority of it would work as is. And that is
a
major objective: to be able to retrofit existing programs to use VSFs
with
a minimum of effort.
By now, I hope you get the gist of all this.
I suppose my questions are:
1) Has this already been done somewhere? It seems like a rather
obvious place to come to. I've found a few "multithreaded javascript"
attempts but the seem to be going about it the wrong way implementing
particular solutions rather than a general solution.
2) Any particular thing above cause your face to melt in fear?
Thank you for your time,
pedz
1) new language
2) new JS library
3) both 1 and 2
4) new way of thinking
Given:
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <[email protected]>
I thought I'd be brave and ask here rather than there.
More and more in browsers the programming model is becoming event
driven. Take a typical Ajax call. My personal thought process is
something like:
1) Send the Ajax request
2) wait for it to get back
3) dig out the piece I need
4) send the next Ajax request
5) wait for it to complete
6) etc.
The problem is that steps 2 and 5 are done by setting up call back
handlers and so the result is that I do not get to "see" the list of
steps.
In today's particular adventure, I'm not doing Ajax requests but
working with Web Workers which have the same event driven
characteristic. I could maybe change my thinking to not mind this at
all but then I tried hooking up JSpec as a testing framework and it
all fell apart. JSpec at its root is very sequenctial. And I'm going
to bump into this same problem almost everywhere I go.
This got me thinking about a JS library which would create a thing. I
don't want to call it a "thread" nor a "context" because both of those
terms have so much meaning. So, for now, I'm going to call it a VSF
for Virtual Stack Frame.
To start, the VSF has a "pc" and a list of "instructions". The pc
starts at 0 and works forward. The instructions are calls to
Javascript ... here is my first weakness ... "functions" ? (I hope
thats right.)
What little I've used JS (most of it via Prototype (blush)), I really
like the bind and bindAsEventHandler so that I can get the "this" set
like I want. So, I was going to allow the user to specify the value
of "this" as well as a list of arguments to pass to the function.
Each of these functions needs a way to get back to the VSF so it would
either be passed as an argument or somehow accessable via the context.
JSpec uses the "with" statement for this but I know that Crockford, in
particular, dislikes the with statement.
The details of how the interface to the functions works has a lot of
options but in general, the idea is that a function can tell the VSF
to not proceed to the next instruction. Instead the VSF would just
exit
(return). For example, each instruction could return true if the next
instruction should be executed and false if not. I suppose it could
be
part of the instruction's specification.
Lets say that an instruction starts an Ajax call. It would set its
completion handler to call vsf.resume where vsf is an instance of a
VSF which initiated the Ajax call. The instruction itself would
return false or do whatever is needed to cause the VSF to not go to
the next instruction.
The call to vsf.resume picks up at the next instruction and continues.
Added entry points to VFS would be to get and set "instance" variables
(as Ruby calls them) and also to chain VSF's so that a stack of VSF's
can be created.
At this point, there is enough power to be dangerous. The user
creates his VSF instances and specifies the list of instructions.
Calls vsf.resume and off it goes.
More power could be added by adding features like being able to muck
with the PC, being able to "return" from the VSF. The other idea was
to implant a list of objects with the connotation that the list of
instructions would be executed once with each object. The
possibilities expand rather quickly which is what brought up point #1
way up at the start of this.
Perhaps, as a second evolution to this, a DSL could be wrapped around
it.
Also, I've been thinking in "single threaded" terms but a VSF could
have a parent concept perhaps called a VPS (Virtual Program Sequence)
which is just a list of instructions. Then a VSF would be started
from a VPS. The idea is that multiple instances of the same VPS could
be running around making a mess of things all at the same time. This
would introduce the need of locking, etc.
But, you ask, what about JSpec? Well, it would need to be redone to
use VSFs but I think the majority of it would work as is. And that is
a
major objective: to be able to retrofit existing programs to use VSFs
with
a minimum of effort.
By now, I hope you get the gist of all this.
I suppose my questions are:
1) Has this already been done somewhere? It seems like a rather
obvious place to come to. I've found a few "multithreaded javascript"
attempts but the seem to be going about it the wrong way implementing
particular solutions rather than a general solution.
2) Any particular thing above cause your face to melt in fear?
Thank you for your time,
pedz