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
  • Data conversion
  • To Native
  • From Native
  • Wrapper
  • JSStringWrapper
  • JSFunctionWrapper
  • JSCallbackWrapper
  • JSScheduleWrapper
  • JSTouchDelegate

Was this helpful?

  1. README

COCOS JSAPI Encapsulation

Data conversion

To Native

bool jsval_to_int32( JSContext *cx, JS::HandleValue vp, int32_t *ret );
bool jsval_to_uint16( JSContext *cx, JS::HandleValue vp, uint16_t *ret );
bool jsval_to_long( JSContext *cx, JS::HandleValue vp, long *out);
bool jsval_to_long_long(JSContext *cx, JS::HandleValue v, long long* ret);
bool jsval_to_ccpoint(JSContext *cx, JS::HandleValue v, cocos2d::Point* ret);
bool jsval_to_ccrect(JSContext *cx, JS::HandleValue v, cocos2d::Rect* ret);
bool jsval_to_ccsize(JSContext *cx, JS::HandleValue v, cocos2d::Size* ret);
bool jsval_to_cccolor4b(JSContext *cx, JS::HandleValue v, cocos2d::Color4B* ret);
bool jsval_to_ccarray(JSContext* cx, JS::HandleValue v, cocos2d::__Array** ret);
bool jsval_to_ccdictionary(JSContext* cx, JS::HandleValue v, cocos2d::__Dictionary** ret);
// ......

From Native

jsval int32_to_jsval( JSContext *cx, int32_t l);
jsval long_to_jsval( JSContext *cx, long number );
jsval long_long_to_jsval(JSContext* cx, long long v);
jsval std_string_to_jsval(JSContext* cx, const std::string& v);
jsval c_string_to_jsval(JSContext* cx, const char* v, size_t length = -1);
jsval ccpoint_to_jsval(JSContext* cx, const cocos2d::Point& v);
jsval ccrect_to_jsval(JSContext* cx, const cocos2d::Rect& v);
jsval ccsize_to_jsval(JSContext* cx, const cocos2d::Size& v);
jsval cccolor4b_to_jsval(JSContext* cx, const cocos2d::Color4B& v);
jsval ccdictionary_to_jsval(JSContext* cx, cocos2d::__Dictionary *dict);
jsval ccarray_to_jsval(JSContext* cx, cocos2d::__Array *arr);
// ......

Wrapper

JSStringWrapper

A simple utility to avoid mem leaking when using JSString

class JSStringWrapper
{
public:
    JSStringWrapper();
    JSStringWrapper(JSString* str, JSContext* cx = NULL);
    JSStringWrapper(jsval val, JSContext* cx = NULL);
    ~JSStringWrapper();
    void set(jsval val, JSContext* cx);
    void set(JSString* str, JSContext* cx);
    const char* get();
private:
    const char* _buffer;
};

JSFunctionWrapper

Wraps a function and this object

class JSFunctionWrapper
{
public:
    JSFunctionWrapper(JSContext* cx, JS::HandleObject jsthis, JS::HandleValue fval);
    ~JSFunctionWrapper();

    bool invoke(unsigned int argc, jsval *argv, JS::MutableHandleValue rval);
private:
    JSContext *_cx;
    mozilla::Maybe<JS::PersistentRootedObject> _jsthis;
    mozilla::Maybe<JS::PersistentRootedValue> _fval;
};

JSCallbackWrapper

class JSCallbackWrapper: public cocos2d::Ref {
public:
    JSCallbackWrapper();
    virtual ~JSCallbackWrapper();
    void setJSCallbackFunc(JS::HandleValue callback);
    void setJSCallbackThis(JS::HandleValue thisObj);
    void setJSExtraData(JS::HandleValue data);

    const jsval getJSCallbackFunc() const;
    const jsval getJSCallbackThis() const;
    const jsval getJSExtraData() const;
protected:
    mozilla::Maybe<JS::PersistentRootedValue> _jsCallback;
    mozilla::Maybe<JS::PersistentRootedValue> _jsThisObj;
    mozilla::Maybe<JS::PersistentRootedValue> _extraData;
};

JSScheduleWrapper

class JSScheduleWrapper: public JSCallbackWrapper {
public:
    JSScheduleWrapper();
    virtual ~JSScheduleWrapper();

    static void setTargetForSchedule(
        JS::HandleValue sched, JSScheduleWrapper *target
    );
    static void setTargetForJSObject(
        JS::HandleObject jsTargetObj, JSScheduleWrapper *target
    );
    // ...
protected:
    Ref* _pTarget;
    mozilla::Maybe<JS::PersistentRootedObject> _pPureJSTarget;
    int _priority;
    bool _isUpdateSchedule;
};

JSTouchDelegate

class JSTouchDelegate: public cocos2d::Ref
{
public:
    JSTouchDelegate();
    ~JSTouchDelegate();

    static void setDelegateForJSObject(
        JSObject* pJSObj, JSTouchDelegate* pDelegate
    );
    void setJSObject(JS::HandleObject obj);
    void registerStandardDelegate(int priority);
    void registerTargetedDelegate(int priority, bool swallowsTouches);
    // ...
private:
    mozilla::Maybe<JS::PersistentRootedObject> _obj;
    typedef std::unordered_map<JSObject*, JSTouchDelegate*> TouchDelegateMap;
    typedef std::pair<JSObject*, JSTouchDelegate*> TouchDelegatePair;
    static TouchDelegateMap sTouchDelegateMap;
    cocos2d::EventListenerTouchOneByOne*  _touchListenerOneByOne;
    cocos2d::EventListenerTouchAllAtOnce* _touchListenerAllAtOnce;
};
PreviousTable of contentNextJSBinding Code Snippets

Last updated 5 years ago

Was this helpful?