diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-08-17 20:23:52 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-08-17 20:23:52 -0700 |
commit | efe09785c319912747f85f8c91fe2430131dc7ea (patch) | |
tree | 45b0f4d6a176226a3eb3078e1860e91a3d8d939e | |
parent | 7258f46b91b6c0eccd1881dd0c95a679d712b959 (diff) |
restructure wrapping in bindings
-rwxr-xr-x | tools/bindings_generator.py | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/tools/bindings_generator.py b/tools/bindings_generator.py index 4e36d032..a4fbb4cc 100755 --- a/tools/bindings_generator.py +++ b/tools/bindings_generator.py @@ -311,7 +311,16 @@ gen_c.write('extern "C" {\n') # Having this object saves us needing to do checks for the object being null each time in the bindings code. gen_js.write(''' // Bindings utilities -function wrapPointer(ptr) { return { ptr: ptr } }; +var Object__cache = {}; +function wrapPointer(ptr, class_) { + var cache = class_ ? class_.prototype.__cache__ : Object__cache; + var ret = cache[ptr]; + if (ret) return ret; + class_ = class_ || Object; + ret = Object.create(class_.prototype); + ret.ptr = ptr; + return cache[ptr] = ret; +}; this['wrapPointer'] = wrapPointer; this['NULL'] = wrapPointer(0); @@ -321,17 +330,15 @@ function destroy(obj) { obj['__destroy__'](); } this['destroy'] = destroy; + +function compare(obj1, obj2) { + return obj1.ptr === obj2.ptr; +} +this['compare'] = compare; ''') def generate_wrapping_code(classname): - return '''var %(classname)s__cache__ = {}; -function %(classname)s__wrap__(ptr) { - var ret = %(classname)s__cache__[ptr]; - if (ret) return ret; - var ret = Object.create(%(classname)s.prototype); - ret.ptr = ptr; - return %(classname)s__cache__[ptr] = ret; -} + return '''%(classname)s.prototype.__cache__ = {}; ''' % { 'classname': classname } # %(classname)s.prototype['fields'] = Runtime.generateStructInfo(null, '%(classname)s'); - consider adding this @@ -495,7 +502,7 @@ def generate_class(generating_classname, classname, clazz): # TODO: deprecate ge print 'zz making return', classname, method['name'], method['returns'], return_value if method['returns'] in classes: # Generate a wrapper - calls += 'return %s__wrap__(%s);' % (method['returns'].split('::')[-1], return_value) + calls += 'return wrapPointer(%s, %s);' % (return_value, method['returns'].split('::')[-1]) else: # Normal return calls += ('return ' if ret != 'void' else '') + return_value + ';' |