|
| 1 | +#include "BehaviorSharedDataJsImplementation.h" |
| 2 | +#include <GDCore/IDE/Dialogs/PropertyDescriptor.h> |
| 3 | +#include <GDCore/Project/Behavior.h> |
| 4 | +#include <GDCore/Project/Project.h> |
| 5 | +#include <GDCore/Serialization/Serializer.h> |
| 6 | +#include <GDCore/Serialization/SerializerElement.h> |
| 7 | +#include <emscripten.h> |
| 8 | +#include <map> |
| 9 | + |
| 10 | +using namespace gd; |
| 11 | + |
| 12 | +std::shared_ptr<gd::BehaviorsSharedData> |
| 13 | +BehaviorSharedDataJsImplementation::Clone() const { |
| 14 | + BehaviorSharedDataJsImplementation* clone = |
| 15 | + new BehaviorSharedDataJsImplementation(*this); |
| 16 | + |
| 17 | + // Copy the references to the JS implementations of the functions (because we |
| 18 | + // want an object cloned from C++ to retain the functions implemented in JS). |
| 19 | + EM_ASM_INT( |
| 20 | + { |
| 21 | + var clone = Module['wrapPointer']( |
| 22 | + $0, Module['BehaviorSharedDataJsImplementation']); |
| 23 | + var self = Module['wrapPointer']( |
| 24 | + $1, Module['BehaviorSharedDataJsImplementation']); |
| 25 | + |
| 26 | + clone['getProperties'] = self['getProperties']; |
| 27 | + clone['updateProperty'] = self['updateProperty']; |
| 28 | + }, |
| 29 | + (int)clone, |
| 30 | + (int)this); |
| 31 | + |
| 32 | + return std::shared_ptr<gd::BehaviorsSharedData>(clone); |
| 33 | +} |
| 34 | +std::map<gd::String, gd::PropertyDescriptor> |
| 35 | +BehaviorSharedDataJsImplementation::GetProperties(gd::Project&) const { |
| 36 | + std::map<gd::String, gd::PropertyDescriptor>* jsCreatedProperties = nullptr; |
| 37 | + std::map<gd::String, gd::PropertyDescriptor> copiedProperties; |
| 38 | + |
| 39 | + jsCreatedProperties = (std::map<gd::String, gd::PropertyDescriptor>*)EM_ASM_INT( |
| 40 | + { |
| 41 | + var self = Module['getCache']( |
| 42 | + Module['BehaviorSharedDataJsImplementation'])[$0]; |
| 43 | + if (!self.hasOwnProperty('getProperties')) |
| 44 | + throw 'getProperties is not defined on a BehaviorSharedDataJsImplementation.'; |
| 45 | + |
| 46 | + var objectContent = JSON.parse(Pointer_stringify($1)); |
| 47 | + var newProperties = self['getProperties'](objectContent); |
| 48 | + if (!newProperties) |
| 49 | + throw 'getProperties returned nothing in a gd::BehaviorSharedDataJsImplementation.'; |
| 50 | + |
| 51 | + return getPointer(newProperties); |
| 52 | + }, |
| 53 | + (int)this, |
| 54 | + jsonContent.c_str()); |
| 55 | + |
| 56 | + copiedProperties = *jsCreatedProperties; |
| 57 | + delete jsCreatedProperties; |
| 58 | + return copiedProperties; |
| 59 | +} |
| 60 | +bool BehaviorSharedDataJsImplementation::UpdateProperty(const gd::String& arg0, |
| 61 | + const gd::String& arg1, |
| 62 | + Project&) { |
| 63 | + jsonContent = (const char*)EM_ASM_INT( |
| 64 | + { |
| 65 | + var self = Module['getCache']( |
| 66 | + Module['BehaviorSharedDataJsImplementation'])[$0]; |
| 67 | + if (!self.hasOwnProperty('updateProperty')) |
| 68 | + throw 'updateProperty is not defined on a BehaviorSharedDataJsImplementation.'; |
| 69 | + var objectContent = JSON.parse(Pointer_stringify($1)); |
| 70 | + self['updateProperty']( |
| 71 | + objectContent, Pointer_stringify($2), Pointer_stringify($3)); |
| 72 | + return ensureString(JSON.stringify(objectContent)); |
| 73 | + }, |
| 74 | + (int)this, |
| 75 | + jsonContent.c_str(), |
| 76 | + arg0.c_str(), |
| 77 | + arg1.c_str()); |
| 78 | + |
| 79 | + return true; |
| 80 | +} |
| 81 | + |
| 82 | +void BehaviorSharedDataJsImplementation::SerializeTo( |
| 83 | + SerializerElement& arg0) const { |
| 84 | + arg0.AddChild("content") = gd::Serializer::FromJSON(jsonContent); |
| 85 | +} |
| 86 | +void BehaviorSharedDataJsImplementation::UnserializeFrom( |
| 87 | + const SerializerElement& arg1) { |
| 88 | + jsonContent = gd::Serializer::ToJSON(arg1.GetChild("content")); |
| 89 | +} |
| 90 | + |
| 91 | +void BehaviorSharedDataJsImplementation::__destroy__() { // Useless? |
| 92 | + EM_ASM_INT( |
| 93 | + { |
| 94 | + var self = Module['getCache']( |
| 95 | + Module['BehaviorSharedDataJsImplementation'])[$0]; |
| 96 | + if (!self.hasOwnProperty('__destroy__')) |
| 97 | + throw 'a JSImplementation must implement all functions, you forgot BehaviorSharedDataJsImplementation::__destroy__.'; |
| 98 | + self['__destroy__'](); |
| 99 | + }, |
| 100 | + (int)this); |
| 101 | +} |
0 commit comments