SpiderMonkey Function Calls

View: New views
3 Messages — Rating Filter:   Alert me  

SpiderMonkey Function Calls

by Frank Natoli :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Newbie to Javascript and SpiderMonkey. Trying to understand how a browser
uses SpiderMonkey to invoke Javascript functions, e.g., to start a streaming
audio player. For example, http://www.kfi640.com, radio station in Los
Angeles, has "listen live" buttons that have an href to
"javascript:openStreamingPlayer();". The same home page has multiple
<script> declarations to external JS files, one of which contains the
declaration for "javascript:openStreamingPlayer();". How is the browser to
know which JS file to compile and function call? I believe I understand that
the browser should call JS_CompileScript for each JS script, then use the
JSScript* returned by JS_CompileScript to invoke JS_CallFunctionName. But
how does the browser know which JSScript* object to use if there are many
separately compiled JSScript* objects? What is the best tutorial for
understanding how to use SpiderMonkey? I don't want to write webpages; I
want to interpret them, like a browser.

Assume a single webpage with two <script> references to external
Javascripts. The browser calls JS_NewRuntime to create a JSRuntime* object.
Then calls JS_NewContext to create a JSContext* object based on the
JSRuntime*. Then calls JS_NewObject to create a JSObject* object based on
the JSContext*.

The browser can then compile the first external Javascript by calling
JS_CompileFile returning a JSScript* object. The call to JS_CompileFile
references the above JSContext* and JSObject* objects.

The browser then compiles the second external Javascript as above, getting a
new, different JSScript* object.

Question now is: has the functionality of both external Javascripts been
compiled into the single JSObject*? Thus a call to JS_CallFunctionName, with
arguments JCContext* and JSObject*, will find the named function regardless
of which external Javascript contained it?

If that is true, what is the significance of the JSScript* returned by
JS_CompileFile, if the smarts of the script are placed in the JSObject*?

Thanks for your time.


_______________________________________________
dev-tech-js-engine mailing list
dev-tech-js-engine@...
https://lists.mozilla.org/listinfo/dev-tech-js-engine

Re: SpiderMonkey Function Calls

by Jason Orendorff :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jun 20, 12:59 pm, "Frank Natoli" <frankjnat...@...>
wrote:
> [...] "listen live" buttons that have an href to
> "javascript:openStreamingPlayer();". The same home page has multiple
> <script> declarations to external JS files, one of which contains the
> declaration for "javascript:openStreamingPlayer();". How is the browser to
> know which JS file to compile and function call?

The browser just runs both scripts in the same scope--that is, the
same global object, or window object--using JS_EvaluateScript
(actually JS_EvaluateUCScriptForPrincipals).

> Question now is: has the functionality of both external Javascripts been
> compiled into the single JSObject*?

Pretty much, yes.  When the browser runs the scripts, it creates
JavaScript functions.  Those functions are stored as properties of the
global object.

> Thus a call to JS_CallFunctionName, with
> arguments JCContext* and JSObject*, will find the named function regardless
> of which external Javascript contained it?

Yes.

> If that is true, what is the significance of the JSScript* returned by
> JS_CompileFile, if the smarts of the script are placed in the JSObject*?

The JSScript represents the whole source script, which may contain
many function declarations--and other declarations and statements too.

-j
_______________________________________________
dev-tech-js-engine mailing list
dev-tech-js-engine@...
https://lists.mozilla.org/listinfo/dev-tech-js-engine

Re: SpiderMonkey Function Calls

by FrankNatoli :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Many thanks for your earlier reply. See below C++ code that I've
written to test my understanding of SpiderMonkey. The three JS files
in table "scriptList" were downloaded from http://www.kfi640.com, a
radio station in Los Angeles. The second JS file,
"on_demand_playersT2.js", absolutely positively contains function
"openStreamingPlayer". Hence "openStreamingPlayer" should have been
compiled into "object" by "JS_CompileFile". But the later call to
"JS_CallFunctionName" fails. So what am I missing here? There is a
"Listen Live" button on the HTML page that invokes
"openStreamingPlayer". I'm trying to do the same. What's the trick?
Thanks again.

#include "stdafx.h"
#include "JSDemo.h"
#include "MainFrm.h"
#include "jsapi.h"

char *scriptList[] = {
        "c:\\tmp6\\flashobject.js",
        "c:\\tmp6\\on_demand_playersT2.js",
        "c:\\tmp6\\poll.js"
};

char funcName[] = "openStreamingPlayer";

void CMainFrame::JavascriptTest(void)
{
        CString str;
        jsval rval;

        JSClass global_class = {
                "global",0,
                JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,
                JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub
        };

        JSRuntime *runtime = JS_NewRuntime(0x100000);
        JSContext *context = JS_NewContext(runtime, 0x1000);
        JSObject *object = JS_NewObject(context, &global_class, NULL, NULL);
        JS_InitStandardClasses(context, object);

        for (int ndx = 0; ndx < sizeof(scriptList) / sizeof(char*); ndx++)
        {
                JSScript *script = JS_CompileFile(context, object, scriptList[ndx]);
                if (script)
                        str.Format(_T("JS_CompileFile %S success"), scriptList[ndx]);
                else
                        str.Format(_T("JS_CompileFile %S failure"), scriptList[ndx]);
                AfxMessageBox(str);
        }

        if (JS_CallFunctionName(context, object, funcName, 0, NULL, &rval))
                str.Format(_T("JS_CallFunctionName %S success"), funcName);
        else
                str.Format(_T("JS_CallFunctionName %S failure"), funcName);
        AfxMessageBox(str);

        JS_DestroyContext(context);
        JS_DestroyRuntime(runtime);
}
_______________________________________________
dev-tech-js-engine mailing list
dev-tech-js-engine@...
https://lists.mozilla.org/listinfo/dev-tech-js-engine
LightInTheBox - Buy quality products at wholesale price!