On Jun 13, 6:38*pm, pedz <pedz...@gmail.com> wrote:
> I need either a
>
> *1) new language
And why do you need a new language?
> *2) new JS library
A new JS library to do what?
> *3) both 1 and 2
> *4) new way of thinking
#4 would be my guess.
>
> 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, <f806at$ail$1$8300d...@news.demon.co.uk>
Yes, that's quite evident.
>
> I thought I'd be brave and ask here rather than there.
Brave? Regardless, people who don't know JS are not the best people
to ask about JS.
>
> More and more in browsers the programming model is becoming event
> driven.
Do you mean reliant on asynchronous operations (e.g. XHR, SQL, etc.)?
> Take a typical Ajax call. *My personal thought process is
> something like:
>
> *1) Send the Ajax request
> *2) wait for it to get back
You don't have to wait for it to get back. The request object calls
you when it is ready (or has given up).
> *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.
So you would prefer to use synchronous XHR calls? Unfortunately, they
are unusable as they block the browser UI. Of course, that didn't
stop Dojo.
>
> In today's particular adventure, I'm not doing Ajax requests but
> working with Web Workers which have the same event driven
> characteristic.
In other words, they call back asynchronously.
> 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.
You are either using it incorrectly or it is unsuited to test browser
scripts.
> JSpec at its root is very sequenctial. *And I'm going
> to bump into this same problem almost everywhere I go.
It sounds as if the problem is JSpec.
Patient: Doc, it hurts when I do this.
Doctor: Don't do *that*.
So find another testing framework.
>
> 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.
You'd be hard-pressed to call it a thread as JS implementations are
single-threaded.
The term "context" has no meaning by itself. But add "execution" and
it is well-defined in JS. Well, unless you are the Dojo authors.
dojo.partial = function(/*Function|String*/method /*, ...*/){
// summary:
// similar to hitch() except that the scope object is left to be
// whatever the execution context eventually becomes.
Did they mean "scope" or "execution context"? Of course, the answer
is neither. But they aren't alone in such foolishness. The - this -
object is referred to as "scope" in virtually every blob of JS posted
to the Web in the last decade. I thought it was humorous that the
Dojo "experts" managed to refer to it as both "scope" and "execution
context" in the same sentence. That's certainly raising the bar for
confusion.
> So, for now, I'm going to call it a VSF
> for Virtual Stack Frame.
Okay.
>
> 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.)
Hard to say at this point.
>
> What little I've used JS (most of it via Prototype (blush)),
We all make mistakes. Admitting them is the first step to recovery.
> I really
> like the bind and bindAsEventHandler so that I can get the "this" set
> like I want.
Yes! You aren't as clueless as you think (at least not in comparison
to the authors of most "major" JS libraries).
> 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.
Yes, I believe that is what Prototype calls "bind" (similar to
Function.prototype.bind found in the latest incarnation of ES). Such
a construct can be very useful.
> 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.
I imagine you mean scope.
> JSpec uses the "with" statement for this but I know that Crockford, in
> particular, dislikes the with statement.
Nobody likes it (and for very good reasons), though I did notice it
about a hundred lines in in my review of Dojo 1.4. They are rebels
for sure.
>
> 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).
It sounds like you want a unit testing framework that can handle test
functions that do asynchronous operations. Such things are already
out there. I wrote one over the winter for My Library. In short,
every unit test function receives a callback function from the
framework. When each test completes (synchronously or
asynchronously), it calls that function. The callback function logs
the result and calls the next test function in the queue.
> 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.
Interesting take. If you return true/false to indicate synchronous/
asynchronous, what will indicate success/failure? ISTM it would be
much simpler to have every function use the callback.
>
> 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.
Why not keep it simple, at least for starters? Why do you need any
special "VSF" objects at all? And points off for using the term
"instance" as it doesn't really apply to JS objects (despite the
unfortunately named instanceof operator).
> 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.
See what I mean? The root of your problem is that you are trying to
envision and design a complicated system where a simple one would
suffice. That's a recurring theme for JS projects of all shapes and
sizes (particularly libraries).
>
> Added entry points to VFS would be to get and set "instance" variables
> (as Ruby calls them)
Never mind what Ruby calls them. Who is she anyway?
> and also to chain VSF's so that a stack of VSF's
> can be created.
I don't see why you need one "VSF", let alone a stack of them.
>
> At this point, there is enough power to be dangerous.
My thoughts exactly.
> The user
> creates his VSF instances and specifies the list of instructions.
> Calls vsf.resume and off it goes.
Calls resume to start? It's already confused and it isn't even off
the drawing board.
>
> More power could be added by adding features like being able to muck
> with the PC,
Muck with the PC?
> being able to "return" from the VSF.
You return when the queue is empty, which happens after each of the
queued tests has either completed or timed out. And to alleviate any
confusion, the "queue" I refer to is just an array of test functions.
> 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.
You should settle on one (preferably simple) idea to start with.
> The
> possibilities expand rather quickly which is what brought up point #1
> way up at the start of this.
You are simply over-thinking the problem.
>
> Perhaps, as a second evolution to this, a DSL could be wrapped around
> it.
You have to create something before it can evolve.
>
> 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.)
I think you are going to make a fine mess.
> This
> would introduce the need of locking, etc.
So why introduce it?
>
> But, you ask, what about JSpec?
What about it?
> Well, it would need to be redone to
> use VSFs but I think the majority of it would work as is.
Whatever.
> And that is
> a
> major objective: to be able to retrofit existing programs to use VSFs
> with
> a minimum of effort.
Why?
>
> By now, I hope you get the gist of all this.
Somewhat.
>
> I suppose my questions are:
>
> 1) Has this already been done somewhere?
All of it?! No idea.
> 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.
I think you are barking up the wrong tree with that concept.
>
> 2) Any particular thing above cause your face to melt in fear?
No, but I do feel a headache coming on. Perhaps somebody else can
recommend an existing unit testing framework for JS. ISTM that's
would be the best thing for you at this point. If you like it,
perhaps you can copy and evolve it to fit your grand plans for stacked
VSF's and multi-threaded JS.
Best of luck to you!