OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 result += " if (result)\n"; | 435 result += " if (result)\n"; |
436 result += " {\n"; | 436 result += " {\n"; |
437 | 437 |
438 auto it = classes.find(call.pointerType); | 438 auto it = classes.find(call.pointerType); |
439 if (it == classes.end()) | 439 if (it == classes.end()) |
440 throw std::runtime_error("Function " + std::string(call.name) + " return
s pointer to unknown class"); | 440 throw std::runtime_error("Function " + std::string(call.name) + " return
s pointer to unknown class"); |
441 | 441 |
442 const ClassInfo& cls = it->second; | 442 const ClassInfo& cls = it->second; |
443 auto offset = cls.subclass_differentiator.offset; | 443 auto offset = cls.subclass_differentiator.offset; |
444 if (offset == SIZE_MAX) | 444 if (offset == SIZE_MAX) |
445 result += " result = " + cls.name + "(result);\n"; | 445 result += " result = exports." + cls.name + "(result);\n"; |
446 else | 446 else |
447 { | 447 result += " result = exports." + cls.name + ".fromPointer(result);\n"
; |
448 result += " var type = HEAP32[result + " + std::to_string(offset)+ "
>> 2];\n"; | |
449 result += " if (type in " + cls.name + "_mapping)\n"; | |
450 result += " result = new (exports[" + cls.name + "_mapping[type]])(
result);\n"; | |
451 result += " else\n"; | |
452 result += " throw new Error('Unexpected " + cls.name + " type: ' +
type);\n"; | |
453 } | |
454 | 448 |
455 result += " }\n"; | 449 result += " }\n"; |
456 result += " else\n"; | 450 result += " else\n"; |
457 result += " result = null;\n"; | 451 result += " result = null;\n"; |
458 return result; | 452 return result; |
459 } | 453 } |
460 else | 454 else |
461 throw std::runtime_error("Unexpected return type for " + std::string(call.
name)); | 455 throw std::runtime_error("Unexpected return type for " + std::string(call.
name)); |
462 } | 456 } |
463 | 457 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 { | 555 { |
562 Module._ReleaseRef(this._pointer + ref_counted_offset); | 556 Module._ReleaseRef(this._pointer + ref_counted_offset); |
563 }; | 557 }; |
564 return result; | 558 return result; |
565 } | 559 } |
566 )"); | 560 )"); |
567 } | 561 } |
568 | 562 |
569 void printClass(const ClassInfo& cls) | 563 void printClass(const ClassInfo& cls) |
570 { | 564 { |
| 565 printf("exports.%s = createClass(%s, %i);\n", cls.name.c_str(), |
| 566 (cls.baseClass ? ("exports." + cls.baseClass->name).c_str() : "null"), |
| 567 cls.ref_counted_offset); |
| 568 |
571 DifferentiatorInfo differentiator = cls.subclass_differentiator; | 569 DifferentiatorInfo differentiator = cls.subclass_differentiator; |
572 if (differentiator.offset != SIZE_MAX) | 570 if (differentiator.offset != SIZE_MAX) |
573 { | 571 { |
574 printf("var %s_mapping = \n", cls.name.c_str()); | 572 printf("var %s_mapping = \n", cls.name.c_str()); |
575 puts("{"); | 573 puts("{"); |
576 for (const auto& item : differentiator.mapping) | 574 for (const auto& item : differentiator.mapping) |
577 printf(" %i: '%s',\n", item.first, item.second.c_str()); | 575 printf(" %i: '%s',\n", item.first, item.second.c_str()); |
578 puts("};"); | 576 puts("};"); |
| 577 |
| 578 printf("exports.%s.fromPointer = function(ptr)\n", cls.name.c_str()); |
| 579 puts("{"); |
| 580 printf(" var type = HEAP32[ptr + %i >> 2];\n", differentiator.offset); |
| 581 printf(" if (type in %s_mapping)\n", cls.name.c_str()); |
| 582 printf(" return new (exports[%s_mapping[type]])(ptr);\n", cls.name.c_st
r()); |
| 583 printf(" throw new Error('Unexpected %s type: ' + type);\n", cls.name.c_s
tr()); |
| 584 puts("};"); |
579 } | 585 } |
580 | 586 else |
581 printf("exports.%s = createClass(%s, %i);\n", cls.name.c_str(), | 587 { |
582 (cls.baseClass ? ("exports." + cls.baseClass->name).c_str() : "null"), | 588 printf("exports.%s.fromPointer = function(ptr)\n", cls.name.c_str()); |
583 cls.ref_counted_offset); | 589 puts("{"); |
| 590 printf(" return new exports.%s(ptr);\n", cls.name.c_str()); |
| 591 puts("};"); |
| 592 } |
584 | 593 |
585 for (const auto& item : cls.properties) | 594 for (const auto& item : cls.properties) |
586 { | 595 { |
587 printf("Object.defineProperty(exports.%s.prototype, '%s', {%s});\n", | 596 printf("Object.defineProperty(exports.%s.prototype, '%s', {%s});\n", |
588 cls.name.c_str(), item.name.c_str(), | 597 cls.name.c_str(), item.name.c_str(), |
589 generatePropertyDescriptor(item).c_str()); | 598 generatePropertyDescriptor(item).c_str()); |
590 } | 599 } |
591 | 600 |
592 for (const auto& item : cls.methods) | 601 for (const auto& item : cls.methods) |
593 { | 602 { |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 bindings_internal::register_differentiator( | 760 bindings_internal::register_differentiator( |
752 bindings_internal::TypeInfo<ClassType>(), offset, mapping); | 761 bindings_internal::TypeInfo<ClassType>(), offset, mapping); |
753 return *this; | 762 return *this; |
754 } | 763 } |
755 }; | 764 }; |
756 | 765 |
757 void custom_generator(bindings_internal::CustomGenerator generator) | 766 void custom_generator(bindings_internal::CustomGenerator generator) |
758 { | 767 { |
759 bindings_internal::customGenerators.push_back(generator); | 768 bindings_internal::customGenerators.push_back(generator); |
760 } | 769 } |
OLD | NEW |