gitbook_cocos_jsbinding
  • README
  • LANGS
  • README
    • SpiderMonkey JSAPI Code Snippets
    • SpiderMonkey Fundamentals
    • SpiderMonkey Garbage Collection
    • SpiderMonkey JSAPI
    • COCOS JSBinding Workflow
    • COCOS JSBinding Objects Storage
    • 目录
    • COCOS JSAPI Encapsulation
    • JSBinding Code Snippets
    • SpiderMonkey Rooting API
  • node_modules
    • gitbook-plugin-todo
      • node_modules
        • cheerio
          • CONTRIBUTING
          • node_modules
            • htmlparser2
              • node_modules
                • domhandler
                  • DOMHandler
                • domutils
                  • readme
                • readable-stream
                  • node_modules
                    • core-util-is
                    • isarray
                    • README
                    • README
                • domelementtype
                  • readme
                • entities
                  • entities
            • lodash v3.10.1
            • css-select
              • node_modules
                • domutils
                  • node_modules
                    • domelementtype
                      • readme
                  • readme
                • boolbase
                • css-what
                  • css-what
                • nth-check
            • entities
              • entities
            • dom-serializer
              • node_modules
                • domelementtype
                  • readme
          • History
          • cheerio
        • README
    • gitbook-plugin-toc
      • node_modules
        • marked-toc
          • node_modules
            • Uslug.js
              • node_modules
                • README
                  • LICENSE
            • template
              • node_modules
                • fs-utils
                  • node_modules
                    • JS-YAML - YAML 1.2 parser and serializer for JavaScript
                      • node_modules
                        • README
                          • node_modules
                            • underscore.string
                              • Underscore.string #
                            • README
                          • HISTORY
                        • README
                      • HISTORY
                    • globule
                      • node_modules
                        • minimatch
                          • node_modules
                            • lru cache
                            • sigmund
                        • Glob
                          • node_modules
                            • minimatch
                              • node_modules
                                • lru cache
                                • sigmund
                            • README
                      • test
                        • fixtures
                          • README
                    • graceful-fs
                    • mkdirp
                      • readme
                    • Async.js
                    • README
                  • test
                    • fixtures
                      • foo
                • delims
                • underscore.string
                  • Underscore.string #
            • fs-utils
              • node_modules
                • JS-YAML - YAML 1.2 parser / writer for JavaScript
                  • node_modules
                    • README
                      • CHANGELOG
                      • node_modules
                        • README
                    • README
                  • CHANGELOG
                • delete
                  • node_modules
                    • README
                      • node_modules
                        • Glob
                          • node_modules
                            • inflight
                              • node_modules
                                • wrappy
                            • minimatch
                              • node_modules
                                • brace-expansion
                                  • node_modules
                                    • balanced-match
                                      • LICENSE
                                    • concat-map
                                      • README
                            • README
                            • once
                              • node_modules
                                • wrappy
                            • path-is-absolute
                              • path-is-absolute
                          • changelog
                    • is-path-in-cwd
                      • node_modules
                        • is-path-inside
                          • is-path-inside
                          • node_modules
                            • Is This Path Inside This Other Path?
                      • is-path-in-cwd
                    • is-path-cwd
                      • is-path-cwd
                  • {%= name %} {%= badge("fury") %}
                • is-absolute
                  • node_modules
                    • is-relative
                • globby
                  • node_modules
                    • README
                      • node_modules
                        • once
                          • node_modules
                            • wrappy
                        • minimatch
                          • node_modules
                            • brace-expansion
                              • node_modules
                                • balanced-match
                                  • LICENSE
                                • concat-map
                                  • README
                        • inflight
                          • node_modules
                            • wrappy
                        • README
                    • array-union
                      • node_modules
                        • array-uniq
                          • array-uniq
                      • array-union
                    • array-differ
                      • array-differ
                  • globby
                • Async.js
                • kind-of
                  • {%= name %} {%= badge("fury") %}
                • extend-shallow
                  • node_modules
                    • array-slice
                  • {%= name %} {%= badge("fury") %}
                • relative
                  • {%= name %} {%= badge("fury") %}
                  • node_modules
                    • normalize-path
                      • {%= name %} {%= badge("fury") %}
                • graceful-fs
              • test
                • fixtures
                  • foo
              • {%= name %} {%= badge("fury") %}
            • gray-matter
              • test
                • fixtures
                  • autodetect-javascript
                  • autodetect-no-lang
                  • This page has JSON front matter!
                  • autodetect-coffee
                  • This page has YAML front matter!
                  • autodetect-json
                  • autodetect-yaml
                  • has-matter
                  • to-json
                  • delims-custom
                  • lang-toml
                  • to-yaml
                  • autodetect-coffee-fn
                  • This file doesn't have matter!
                  • autodetect-toml
                  • autodetect-cson-fn
                  • delims-semi-colons
                  • lang-coffee-fn
                  • lang-javascript-fn
                  • complex
                  • lang-cson-fn
                  • This page has cson front matter!
                  • empty
                  • delims-and-lang
                  • This page has coffee front matter!
                  • autodetect-cson
              • {%= name %} {%= badge("fury") %}
              • node_modules
                • README
                  • node_modules
                    • arrayify-compact
                      • node_modules
                        • Array Flatten
                      • {%= name %} {%= badge("fury") %}
                    • object-pick
                • mixin-deep
                  • node_modules
                    • is-plain-object
                      • {%= name %} {%= badge("fury") %}
                    • for-own
                      • node_modules
                        • for-in
                  • {%= name %} {%= badge("fury") %}
                • verbalize
                  • node_modules
                    • chalk
                      • readme
                      • node_modules
                        • ansi-styles
                          • ansi-styles
                        • strip-ansi
                          • strip-ansi
                        • has-color
                          • has-color
                  • docs
                    • install
                    • {%= name %} {%= badge('fury') %}
                • JS-YAML - YAML 1.2 parser / writer for JavaScript
                  • node_modules
                    • README
                      • CHANGELOG
                      • node_modules
                        • README
                    • README
                  • CHANGELOG
                • TOML Parser for Node.js
                  • CHANGELOG
                • README
                  • How to contribute to CoffeeScript
              • docs
                • examples
                • why
                • options
            • marked
            • Lo-Dash v2.4.2
          • test
            • actual
              • Test
              • Handlebars-Helper
              • Related-Grunt-Tasks
              • Repos-List
              • Examples
              • Readme-Repos-List
              • toc
              • Foo
              • Readme3
              • Readme2
              • Readme-Repos
              • Readme1
              • Related-Repos
              • Badge-Mixins
              • Test
            • fixtures
              • repos-list
              • handlebars-helper
              • examples
              • related-grunt-tasks
              • grunt-readme
              • Contains toc stop comment
              • grunt-readme
              • Badges
              • Examples
              • marked-toc
              • Documentation
              • Test
              • grunt-readme
              • grunt-readme
              • Contains extra comment
              • related-repos
              • grunt-readme
    • gitbook-plugin-search-pro
      • node_modules
        • README
          • node_modules
            • nan
              • doc
                • Scopes
                • Strings & Bytes
                • Maybe Types
                • New
                • Object Wrappers
                • V8 internals
                • Persistent references
                • Asynchronous work helpers
                • Converters
                • Buffers
                • JavaScript-accessible methods
                • Script
                • Errors
                • Nan::Callback
                • Miscellaneous V8 Helpers
                • Miscellaneous Node Helpers
              • The MIT License (MIT)
              • README
              • NAN ChangeLog
          • README_EN
          • NodeJieba ChangeLog
        • lodash v3.10.1
    • Disqus integration for GitBook
  • README
    • SpiderMonkey JSAPI
    • COCOS JSBinding Workflow
    • SpiderMonkey Fundamentals
    • COCOS JSBinding Objects Storage
    • SpiderMonkey Garbage Collection
    • カタログ
    • SpiderMonkey Rooting API
    • SpiderMonkey JSAPI Code Snippets
    • COCOS JSAPI Encapsulation
    • JSBinding Code Snippets
  • README
    • Table of content
    • COCOS JSAPI Encapsulation
    • JSBinding Code Snippets
    • SpiderMonkey Rooting API
    • SpiderMonkey JSAPI Code Snippets
    • SpiderMonkey JSAPI
    • SpiderMonkey Fundamentals
    • COCOS JSBinding Workflow
    • SpiderMonkey Garbage Collection
    • COCOS JSBinding Objects Storage
Powered by GitBook
On this page
  • SpiderMonkey
  • Runtimes
  • Contexts
  • Global Objects
  • Compartments
  • Cross-Compartment Call

Was this helpful?

  1. README

SpiderMonkey Fundamentals

SpiderMonkey

  • Mozilla's JavaScript engine written in C/C++

  • Compiles and executes scripts containing JavaScript statements and functions.

  • Handles memory allocation for the objects needed to execute scripts, and cleans up—garbage collects—objects no longer needs.

Runtimes

JSRuntime

  • The space in which the JavaScript variables, objects, scripts, and contexts allocated.

  • JSContext and object lives within a JSRuntime and cannot travel to other runtimes or be shared across runtimes.

  • Most applications only need one runtime.

C++ Cocos

/* 
    ScriptingCore.h
*/
JSRuntime *_rt;

/* 
    ScriptingCore.cpp
*/
_rt = JS_NewRuntime(8L * 1024L * 1024L);
JS_SetGCParameter(_rt, JSGC_MAX_BYTES, 0xffffffff);
JS_SetTrustedPrincipals(_rt, &shellTrustedPrincipals);
JS_SetSecurityCallbacks(_rt, &securityCallbacks);
JS_SetNativeStackQuota(_rt, JSB_MAX_STACK_QUOTA);
JS::RuntimeOptionsRef(_rt).setIon(true);
JS::RuntimeOptionsRef(_rt).setBaseline(true);
// ......

Contexts

JSContext

  • Do things involving JavaScript code and objects.

    • Compile and execute scripts

    • Get and set object properties

    • Call JavaScript functions

    • Convert JavaScript data from one type to another

    • Create objects

  • JSAPI functions often require a JSContext * as the argument

  • Represents the execution of JS code.

  • Contains a JS stack and is associated with a runtime.

C++ Cocos

/* 
    ScriptingCore.h
*/
JSContext *_cx;

/* 
    ScriptingCore.cpp
*/
_cx = JS_NewContext(_rt, 8192);

Global Objects

mozilla::Maybe<JS::PersistentRootedObject>

  • Contains all the classes, functions, and variables available for JavaScript code to use.

  • JSAPI applications have full control over what global properties scripts can see.

  • Application starts out by creating an object and populating it with the standard JavaScript classes, like Array and Object. Then adds custom classes, functions, and variables.

C++ Cocos

/* 
    ScriptingCore.h
*/
mozilla::Maybe<JS::PersistentRootedObject> _global;

/* 
    ScriptingCore.cpp
*/
_global.construct(_cx);
_global.ref() = NewGlobalObject(_cx);
JSAutoCompartment ac(_cx, _global.ref());
js::SetDefaultObjectForContext(_cx, _global.ref());

Compartments

JSCompartments

  • JSContexts are control, JSCompartments are data.

  • JSCompartment is a memory space that objects and other GCthings are stored within.

  • JSContext is associated with a single JSCompartment at all times.

  • JSContext is "running inside" the associated JSCompartment.

  • Any object created with the JSContext will be physically stored within the context's current JSCompartment.

Cross-Compartment Call

To access data in another JSCompartment.

  • JSContext must first "enter" that other JSCompartment.

  • A JSContext has a field cx->compartment that gives the current compartment.

  • To make a cross-compartment call, cx->compartment is updated to the new compartment.

Only objects in the current compartment can be accessed, so to access an object in a different compartment, this containing compartment has to be entered first. Compartments have to be entered and left in LIFO order.

JSAutoCompartmentguarantees that by automatically entering the given compartment and leaving it upon getting out of scope:

C++

void foo(JSContext *cx, JObject *obj) {
    // in some compartment 'c'
    {
        JSAutoCompartment ac(cx, obj);  // constructor enters
        // in the compartment of 'obj'
    }                                 // destructor leaves
    // back in compartment 'c'
}
PreviousCOCOS JSBinding WorkflowNextCOCOS JSBinding Objects Storage

Last updated 5 years ago

Was this helpful?