| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "MsXpS/libXpertMassCore/XpertMassCoreJavaScript.hpp" | ||
| 2 | |||
| 3 | namespace MsXpS | ||
| 4 | { | ||
| 5 | namespace libXpertMassCore | ||
| 6 | { | ||
| 7 | |||
| 8 | // This function needs to be called this way: | ||
| 9 | // MsXpS::libXpertMassCore::registerEnumsToQJSEngine(engine_p) | ||
| 10 | // in the program or library linking to this library. | ||
| 11 | void | ||
| 12 | ✗ | registerEnumsToQJSEngine(QJSEngine *engine) | |
| 13 | { | ||
| 14 | // qDebug() << "Now registering the Enums:: enums for MsXpS::libXpertMassCore"; | ||
| 15 | |||
| 16 | // Get the meta-object for the namespace (generated by Q_NAMESPACE) | ||
| 17 | ✗ | const QMetaObject *metaObject = &Enums::staticMetaObject; | |
| 18 | ✗ | if(metaObject == nullptr) | |
| 19 | qFatal() << "Programming error. Failed to obtain &Enums::staticMetaObject"; | ||
| 20 | |||
| 21 | // qDebug() << "Found namespace Enums with full name:" | ||
| 22 | // << metaObject->className(); | ||
| 23 | |||
| 24 | ✗ | QJSValue libraryJsRootProperty; | |
| 25 | |||
| 26 | ✗ | if(engine->globalObject().hasProperty("libXpertMassCore")) | |
| 27 | { | ||
| 28 | // qDebug() << "Global object property 'libXpertMassCore' already exists."; | ||
| 29 | ✗ | libraryJsRootProperty = engine->globalObject().property("libXpertMassCore"); | |
| 30 | } | ||
| 31 | else | ||
| 32 | { | ||
| 33 | // qDebug() << "Global object property 'libXpertMassCore' not found."; | ||
| 34 | // Create a JS object for libXpertMassCore | ||
| 35 | ✗ | libraryJsRootProperty = engine->newObject(); | |
| 36 | } | ||
| 37 | |||
| 38 | // Create a nested JS object for Enums | ||
| 39 | ✗ | QJSValue all_js_enums = engine->newObject(); | |
| 40 | |||
| 41 | // Loop through all enums and populate jsEnums | ||
| 42 | ✗ | for(int i = 0; i < metaObject->enumeratorCount(); ++i) | |
| 43 | { | ||
| 44 | ✗ | QMetaEnum iter_meta_enum = metaObject->enumerator(i); | |
| 45 | |||
| 46 | // qDebug() << "Now iterating in enum:" << iter_meta_enum.enumName(); | ||
| 47 | |||
| 48 | ✗ | QJSValue single_js_enum = engine->newObject(); | |
| 49 | |||
| 50 | ✗ | for(int j = 0; j < iter_meta_enum.keyCount(); ++j) | |
| 51 | { | ||
| 52 | // qDebug() << "Now iterating in new key:" << iter_meta_enum.key(j) | ||
| 53 | // << "with value:" << iter_meta_enum.value(j); | ||
| 54 | |||
| 55 | // key() would be "LEFT", with value() = 1 | ||
| 56 | ✗ | single_js_enum.setProperty(iter_meta_enum.key(j), | |
| 57 | iter_meta_enum.value(j)); | ||
| 58 | } | ||
| 59 | |||
| 60 | // name would be CapType | ||
| 61 | ✗ | all_js_enums.setProperty(iter_meta_enum.enumName(), single_js_enum); | |
| 62 | ✗ | } | |
| 63 | |||
| 64 | // Attach Enums to libXpertMassCore | ||
| 65 | ✗ | libraryJsRootProperty.setProperty("Enums", all_js_enums); | |
| 66 | |||
| 67 | ✗ | engine->globalObject().setProperty("libXpertMassCore", libraryJsRootProperty); | |
| 68 | |||
| 69 | #if 0 | ||
| 70 | |||
| 71 | qDebug() | ||
| 72 | << "Now checking for correct exposition of Enums::enum::<key/value> pairs."; | ||
| 73 | |||
| 74 | QJSValue jsEnums = | ||
| 75 | engine->globalObject().property("libXpertMassCore").property("Enums"); | ||
| 76 | |||
| 77 | if(jsEnums.isUndefined()) | ||
| 78 | { | ||
| 79 | qDebug() << "Error: libXpertMassCore.Enums not found!"; | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | else | ||
| 83 | { | ||
| 84 | qDebug() << "libXpertMassCore.Enums JS object exists"; | ||
| 85 | |||
| 86 | // Get all enum names (properties of the Enums object) | ||
| 87 | QJSValue propNames = | ||
| 88 | engine->evaluate("Object.getOwnPropertyNames(libXpertMassCore.Enums)"); | ||
| 89 | |||
| 90 | if(propNames.isError()) | ||
| 91 | { | ||
| 92 | qDebug() << "JS Error:" << propNames.toString(); | ||
| 93 | return; | ||
| 94 | } | ||
| 95 | |||
| 96 | // Iterate through each enum | ||
| 97 | QStringList enumNames = propNames.toVariant().toStringList(); | ||
| 98 | for(const QString &enumName : enumNames) | ||
| 99 | { | ||
| 100 | QJSValue jsEnum = jsEnums.property(enumName); | ||
| 101 | |||
| 102 | if(!jsEnum.isObject()) | ||
| 103 | { | ||
| 104 | qDebug() << "Skipping non-object property:" << enumName; | ||
| 105 | continue; | ||
| 106 | } | ||
| 107 | |||
| 108 | qDebug() << "\nChecking enum:" << enumName; | ||
| 109 | |||
| 110 | // Get all keys of the iterated enum | ||
| 111 | QJSValue enumKeys = engine->evaluate( | ||
| 112 | QString("Object.getOwnPropertyNames(libXpertMassCore.Enums.%1)") | ||
| 113 | .arg(enumName)); | ||
| 114 | |||
| 115 | if(enumKeys.isError()) | ||
| 116 | { | ||
| 117 | qDebug() << " JS Error:" << enumKeys.toString(); | ||
| 118 | continue; | ||
| 119 | } | ||
| 120 | |||
| 121 | // Print key-value pairs for the iterated enum | ||
| 122 | QStringList keys = enumKeys.toVariant().toStringList(); | ||
| 123 | for(const QString &key : keys) | ||
| 124 | { | ||
| 125 | QJSValue value = jsEnum.property(key); | ||
| 126 | qDebug() << " " << key << "=" << value.toInt(); | ||
| 127 | qDebug().noquote() << "\nChecked enum: " << enumName << "member (" | ||
| 128 | << key << " / " << value.toInt() << ")"; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | #endif | ||
| 133 | ✗ | } | |
| 134 | |||
| 135 | // This function needs to be called this way: | ||
| 136 | // MsXpS::libXpertMassCore::registerGlobalsToQJSEngine(engine_p) | ||
| 137 | // in the program or library that links to this library. | ||
| 138 | void | ||
| 139 | ✗ | registerGlobalsToQJSEngine(QJSEngine *engine) | |
| 140 | { | ||
| 141 | ✗ | QJSValue libraryJsRootProperty; | |
| 142 | |||
| 143 | ✗ | if(engine->globalObject().hasProperty("libXpertMassCore")) | |
| 144 | { | ||
| 145 | // qDebug() << "Global object property 'libXpertMassCore' already exists."; | ||
| 146 | ✗ | libraryJsRootProperty = engine->globalObject().property("libXpertMassCore"); | |
| 147 | } | ||
| 148 | else | ||
| 149 | { | ||
| 150 | // qDebug() << "Global object property 'libXpertMassCore' not found."; | ||
| 151 | // Create a JS object for libXpertMassCore | ||
| 152 | ✗ | libraryJsRootProperty = engine->newObject(); | |
| 153 | } | ||
| 154 | |||
| 155 | // Create a nested JS object for Globals | ||
| 156 | ✗ | QJSValue js_globals = engine->newObject(); | |
| 157 | |||
| 158 | ✗ | js_globals.setProperty("ATOM_DEC_PLACES", ATOM_DEC_PLACES); | |
| 159 | ✗ | js_globals.setProperty("OLIGOMER_DEC_PLACES", OLIGOMER_DEC_PLACES); | |
| 160 | ✗ | js_globals.setProperty("POLYMER_DEC_PLACES", POLYMER_DEC_PLACES); | |
| 161 | ✗ | js_globals.setProperty("PKA_PH_PI_DEC_PLACES", PKA_PH_PI_DEC_PLACES); | |
| 162 | |||
| 163 | ✗ | libraryJsRootProperty.setProperty("Globals", js_globals); | |
| 164 | |||
| 165 | ✗ | engine->globalObject().setProperty("libXpertMassCore", libraryJsRootProperty); | |
| 166 | |||
| 167 | ✗ | qRegisterMetaType<MsXpS::libXpertMassCore::ErrorList>("ErrorList"); | |
| 168 | ✗ | } | |
| 169 | |||
| 170 | } // namespace libXpertMassCore | ||
| 171 | } // namespace MsXpS | ||
| 172 |