| 1729 | } |
| 1730 | |
| 1731 | tsn_module* tsn_new_module(tsn_vm* ctx, size_t atoms_length, size_t constants_length, size_t prop_cache_slots) { |
| 1732 | auto jsModule = JS_NewObjectClass(ctx, getTSNModuleClassID()); |
| 1733 | if (tsn_is_exception(ctx, jsModule)) { |
| 1734 | return nullptr; |
| 1735 | } |
| 1736 | |
| 1737 | auto* module = reinterpret_cast<tsn_module*>(js_malloc(ctx, |
| 1738 | sizeof(tsn_module) + (sizeof(tsn_atom) * atoms_length) + |
| 1739 | sizeof(tsn_value) * constants_length + |
| 1740 | sizeof(tsn_prop_cache) * prop_cache_slots)); |
| 1741 | module->name = tsn_get_vm_helpers(ctx)->current_module_name; |
| 1742 | module->atoms_length = atoms_length; |
| 1743 | module->constants_length = constants_length; |
| 1744 | module->prop_cache_slots = prop_cache_slots; |
| 1745 | module->module_as_value = jsModule; |
| 1746 | // Atoms point right after the tsn_module struct |
| 1747 | module->atoms = reinterpret_cast<tsn_atom*>(reinterpret_cast<uint8_t*>(module) + sizeof(tsn_module)); |
| 1748 | // Constants point after the atoms |
| 1749 | module->constants = reinterpret_cast<tsn_value*>(reinterpret_cast<uint8_t*>(module) + sizeof(tsn_module) + |
| 1750 | (sizeof(tsn_atom) * atoms_length)); |
| 1751 | module->prop_cache = |
| 1752 | reinterpret_cast<tsn_prop_cache*>(reinterpret_cast<uint8_t*>(module) + sizeof(tsn_module) + |
| 1753 | (sizeof(tsn_atom) * atoms_length) + (sizeof(tsn_value) * constants_length)); |
| 1754 | for (size_t i = 0; i < atoms_length; i++) { |
| 1755 | module->atoms[i] = 0; |
| 1756 | } |
| 1757 | for (size_t i = 0; i < constants_length; i++) { |
| 1758 | module->constants[i] = JS_UNINITIALIZED; |
| 1759 | } |
| 1760 | for (size_t i = 0; i < prop_cache_slots; i++) { |
| 1761 | memset(&module->prop_cache[i], 0, sizeof(tsn_prop_cache)); |
| 1762 | } |
| 1763 | |
| 1764 | JS_SetOpaque(jsModule, module); |
| 1765 | |
| 1766 | return module; |
| 1767 | } |
| 1768 | |
| 1769 | void tsn_free_module(tsn_vm* ctx, tsn_module* module) { |
| 1770 | if (module != nullptr) { |