LEFT | RIGHT |
1 #pragma once | 1 #pragma once |
2 | 2 |
| 3 #include <cstddef> |
3 #include <cstdint> | 4 #include <cstdint> |
4 #include <cstdio> | 5 #include <cstdio> |
5 #include <cstdlib> | 6 #include <cstdlib> |
6 #include <exception> | 7 #include <exception> |
7 #include <map> | 8 #include <map> |
8 #include <string> | 9 #include <string> |
9 #include <type_traits> | 10 #include <type_traits> |
10 #include <utility> | 11 #include <utility> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 }; | 271 }; |
271 | 272 |
272 struct ClassInfo | 273 struct ClassInfo |
273 { | 274 { |
274 ClassInfo* baseClass; | 275 ClassInfo* baseClass; |
275 std::string name; | 276 std::string name; |
276 std::vector<PropertyInfo> properties; | 277 std::vector<PropertyInfo> properties; |
277 std::vector<MethodInfo> methods; | 278 std::vector<MethodInfo> methods; |
278 std::vector<FunctionInfo> initializers; | 279 std::vector<FunctionInfo> initializers; |
279 DifferentiatorInfo subclass_differentiator; | 280 DifferentiatorInfo subclass_differentiator; |
| 281 ptrdiff_t ref_counted_offset; |
280 }; | 282 }; |
281 | 283 |
282 std::map<TYPEID, ClassInfo> classes; | 284 std::map<TYPEID, ClassInfo> classes; |
283 | 285 |
284 void register_class(const char* name, TYPEID classID, TYPEID baseClassID) | 286 void register_class(const char* name, TYPEID classID, TYPEID baseClassID, |
| 287 ptrdiff_t ref_counted_offset) |
285 { | 288 { |
286 auto it = classes.find(classID); | 289 auto it = classes.find(classID); |
287 if (it != classes.end()) | 290 if (it != classes.end()) |
288 throw std::runtime_error(std::string("Duplicate definition for class ") +
name); | 291 throw std::runtime_error(std::string("Duplicate definition for class ") +
name); |
289 | 292 |
290 ClassInfo* baseClass = nullptr; | 293 ClassInfo* baseClass = nullptr; |
291 if (baseClassID != TypeInfo<NoBaseClass>()) | 294 if (baseClassID != TypeInfo<NoBaseClass>()) |
292 { | 295 { |
293 it = classes.find(baseClassID); | 296 it = classes.find(baseClassID); |
294 if (it == classes.end()) | 297 if (it == classes.end()) |
295 throw std::runtime_error(std::string("Unknown base class defined for cla
ss ") + name); | 298 throw std::runtime_error(std::string("Unknown base class defined for cla
ss ") + name); |
296 baseClass = &(it->second); | 299 baseClass = &(it->second); |
297 } | 300 } |
298 | 301 |
299 ClassInfo classInfo; | 302 ClassInfo classInfo; |
300 classInfo.baseClass = baseClass; | 303 classInfo.baseClass = baseClass; |
301 classInfo.name = name; | 304 classInfo.name = name; |
302 classInfo.subclass_differentiator.offset = SIZE_MAX; | 305 classInfo.subclass_differentiator.offset = SIZE_MAX; |
| 306 classInfo.ref_counted_offset = ref_counted_offset; |
303 classes[classID] = classInfo; | 307 classes[classID] = classInfo; |
304 } | 308 } |
305 | 309 |
306 void register_property(TYPEID classID, const char* name, | 310 void register_property(TYPEID classID, const char* name, |
307 const FunctionInfo& getter, const FunctionInfo& setter, | 311 const FunctionInfo& getter, const FunctionInfo& setter, |
308 const char* jsValue = "") | 312 const char* jsValue = "") |
309 { | 313 { |
310 auto it = classes.find(classID); | 314 auto it = classes.find(classID); |
311 if (it == classes.end()) | 315 if (it == classes.end()) |
312 throw std::runtime_error(std::string("Property defined on unknown class: "
) + name); | 316 throw std::runtime_error(std::string("Property defined on unknown class: "
) + name); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 return result; | 509 return result; |
506 } | 510 } |
507 | 511 |
508 function readString(str) | 512 function readString(str) |
509 { | 513 { |
510 var length = Module._GetStringLength(str); | 514 var length = Module._GetStringLength(str); |
511 var pointer = Module._GetStringData(str) >> 1; | 515 var pointer = Module._GetStringData(str) >> 1; |
512 return String.fromCharCode.apply(String, HEAP16.slice(pointer, pointer + len
gth)); | 516 return String.fromCharCode.apply(String, HEAP16.slice(pointer, pointer + len
gth)); |
513 } | 517 } |
514 | 518 |
515 function createClass(superclass) | 519 function createClass(superclass, ref_counted_offset) |
516 { | 520 { |
517 var result = function(pointer) | 521 var result = function(pointer) |
518 { | 522 { |
519 this._pointer = pointer; | 523 this._pointer = pointer; |
520 }; | 524 }; |
521 if (superclass) | 525 if (superclass) |
522 result.prototype = Object.create(superclass.prototype); | 526 result.prototype = Object.create(superclass.prototype); |
523 result.prototype.delete = function() | 527 result.prototype.delete = function() |
524 { | 528 { |
525 Module._ReleaseRef(this._pointer); | 529 Module._ReleaseRef(this._pointer + ref_counted_offset); |
526 }; | 530 }; |
527 return result; | 531 return result; |
528 })"); | 532 })"); |
529 } | 533 } |
530 | 534 |
531 void printClass(const ClassInfo& cls) | 535 void printClass(const ClassInfo& cls) |
532 { | 536 { |
533 DifferentiatorInfo differentiator = cls.subclass_differentiator; | 537 DifferentiatorInfo differentiator = cls.subclass_differentiator; |
534 if (differentiator.offset != SIZE_MAX) | 538 if (differentiator.offset != SIZE_MAX) |
535 { | 539 { |
536 printf("var %s_mapping = \n", cls.name.c_str()); | 540 printf("var %s_mapping = \n", cls.name.c_str()); |
537 puts("{"); | 541 puts("{"); |
538 for (const auto& item : differentiator.mapping) | 542 for (const auto& item : differentiator.mapping) |
539 printf(" %i: '%s',\n", item.first, item.second.c_str()); | 543 printf(" %i: '%s',\n", item.first, item.second.c_str()); |
540 puts("};"); | 544 puts("};"); |
541 } | 545 } |
542 | 546 |
543 printf("exports.%s = createClass(%s);\n", cls.name.c_str(), | 547 printf("exports.%s = createClass(%s, %i);\n", cls.name.c_str(), |
544 (cls.baseClass ? ("exports." + cls.baseClass->name).c_str() : "")); | 548 (cls.baseClass ? ("exports." + cls.baseClass->name).c_str() : "null"), |
| 549 cls.ref_counted_offset); |
545 | 550 |
546 for (const auto& item : cls.properties) | 551 for (const auto& item : cls.properties) |
547 { | 552 { |
548 printf("Object.defineProperty(exports.%s.prototype, '%s', {%s});\n", | 553 printf("Object.defineProperty(exports.%s.prototype, '%s', {%s});\n", |
549 cls.name.c_str(), item.name.c_str(), | 554 cls.name.c_str(), item.name.c_str(), |
550 generatePropertyDescriptor(item).c_str()); | 555 generatePropertyDescriptor(item).c_str()); |
551 } | 556 } |
552 | 557 |
553 for (const auto& item : cls.methods) | 558 for (const auto& item : cls.methods) |
554 { | 559 { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 String::size_type EMSCRIPTEN_KEEPALIVE GetStringLength(\ | 623 String::size_type EMSCRIPTEN_KEEPALIVE GetStringLength(\ |
619 const String& str)\ | 624 const String& str)\ |
620 {\ | 625 {\ |
621 return str.length();\ | 626 return str.length();\ |
622 }\ | 627 }\ |
623 const String::value_type* EMSCRIPTEN_KEEPALIVE GetStringData(\ | 628 const String::value_type* EMSCRIPTEN_KEEPALIVE GetStringData(\ |
624 const String& str)\ | 629 const String& str)\ |
625 {\ | 630 {\ |
626 return str.data();\ | 631 return str.data();\ |
627 }\ | 632 }\ |
628 void EMSCRIPTEN_KEEPALIVE AddRef(ref_counted* ptr)\ | |
629 {\ | |
630 ptr->AddRef();\ | |
631 }\ | |
632 void EMSCRIPTEN_KEEPALIVE ReleaseRef(ref_counted* ptr)\ | 633 void EMSCRIPTEN_KEEPALIVE ReleaseRef(ref_counted* ptr)\ |
633 {\ | 634 {\ |
634 ptr->ReleaseRef();\ | 635 ptr->ReleaseRef();\ |
635 }\ | 636 }\ |
636 }\ | 637 }\ |
637 void BindingsInitializer_dummy() | 638 void BindingsInitializer_dummy() |
638 #endif | 639 #endif |
639 | 640 |
640 template<typename ClassType, | 641 template<typename ClassType, |
641 typename BaseClass = bindings_internal::NoBaseClass, | 642 typename BaseClass = bindings_internal::NoBaseClass, |
642 typename std::enable_if<std::is_base_of<ref_counted, ClassType>::value>::typ
e* = nullptr> | 643 typename std::enable_if<std::is_base_of<ref_counted, ClassType>::value>::typ
e* = nullptr> |
643 class class_ | 644 class class_ |
644 { | 645 { |
645 public: | 646 public: |
646 class_(const char* name) | 647 class_(const char* name) |
647 { | 648 { |
| 649 ClassType* ptr = reinterpret_cast<ClassType*>(0x10000000); |
| 650 ptrdiff_t ref_counted_offset = |
| 651 reinterpret_cast<char*>(static_cast<ref_counted*>(ptr)) - |
| 652 reinterpret_cast<char*>(ptr); |
648 bindings_internal::register_class(name, | 653 bindings_internal::register_class(name, |
649 bindings_internal::TypeInfo<ClassType>(), | 654 bindings_internal::TypeInfo<ClassType>(), |
650 bindings_internal::TypeInfo<BaseClass>()); | 655 bindings_internal::TypeInfo<BaseClass>(), |
| 656 ref_counted_offset |
| 657 ); |
651 } | 658 } |
652 | 659 |
653 template<typename FieldType> | 660 template<typename FieldType> |
654 const class_& property(const char* name, | 661 const class_& property(const char* name, |
655 FieldType (ClassType::*getter)() const, | 662 FieldType (ClassType::*getter)() const, |
656 void (ClassType::*setter)(FieldType) = nullptr) const | 663 void (ClassType::*setter)(FieldType) = nullptr) const |
657 { | 664 { |
658 bindings_internal::register_property( | 665 bindings_internal::register_property( |
659 bindings_internal::TypeInfo<ClassType>(), name, getter, setter); | 666 bindings_internal::TypeInfo<ClassType>(), name, getter, setter); |
660 return *this; | 667 return *this; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
711 | 718 |
712 std::vector<std::pair<int, std::string>> mapping; | 719 std::vector<std::pair<int, std::string>> mapping; |
713 for (const auto& item : list) | 720 for (const auto& item : list) |
714 mapping.emplace_back(item.first, item.second); | 721 mapping.emplace_back(item.first, item.second); |
715 | 722 |
716 bindings_internal::register_differentiator( | 723 bindings_internal::register_differentiator( |
717 bindings_internal::TypeInfo<ClassType>(), offset, mapping); | 724 bindings_internal::TypeInfo<ClassType>(), offset, mapping); |
718 return *this; | 725 return *this; |
719 } | 726 } |
720 }; | 727 }; |
LEFT | RIGHT |