| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- //
- // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include <Atomic/IO/Log.h>
- #include <Atomic/IO/File.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Resource/JSONFile.h>
- #include "Atomic/Core/ProcessUtils.h"
- #include "JSBind.h"
- #include "JSBPackage.h"
- #include "JSBModule.h"
- #include "JSBHeader.h"
- #include "JSBClass.h"
- #include "JSBEnum.h"
- #include "JSBModuleWriter.h"
- #include "JSBType.h"
- #include "JavaScript/JSModuleWriter.h"
- #include "CSharp/CSModuleWriter.h"
- namespace ToolCore
- {
- JSBModule::JSBModule(Context* context, JSBPackage* package) : Object(context),
- package_(package),
- dotNetModule_(false)
- {
- }
- JSBModule::~JSBModule()
- {
- }
- Vector<SharedPtr<JSBClass>> JSBModule::GetClasses()
- {
- return classes_.Values();
- }
- Vector<SharedPtr<JSBEnum>> JSBModule::GetEnums()
- {
- return enums_.Values();
- }
- void JSBModule::PreprocessHeaders()
- {
- for (unsigned i = 0; i < headers_.Size(); i++)
- {
- headers_[i]->VisitPreprocess();
- }
- }
- void JSBModule::VisitHeaders()
- {
- for (unsigned i = 0; i < headers_.Size(); i++)
- {
- headers_[i]->VisitHeader();
- }
- // validate that all classes found
- for (unsigned i = 0; i < classnames_.Size(); i++)
- {
- JSBClass* cls = GetClass(classnames_[i]);
- if (!cls)
- {
- ErrorExit(ToString("Module class not found %s", classnames_[i].CString()));
- }
- }
- ProcessOverloads();
- ProcessExcludes();
- ProcessClassExcludes();
- ProcessTypeScriptDecl();
- ProcessHaxeDecl();
- }
- void JSBModule::PreprocessClasses()
- {
- HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
- for (itr = classes_.Begin(); itr != classes_.End(); itr++)
- {
- itr->second_->Preprocess();
- }
- }
- void JSBModule::ProcessClasses()
- {
- HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
- for (itr = classes_.Begin(); itr != classes_.End(); itr++)
- {
- itr->second_->Process();
- }
- }
- void JSBModule::PostProcessClasses()
- {
- HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
- for (itr = classes_.Begin(); itr != classes_.End(); itr++)
- {
- itr->second_->PostProcess();
- }
- }
- void JSBModule::ProcessOverloads()
- {
- // overloads
- JSONValue root = moduleJSON_->GetRoot();
- JSONValue overloads = root.Get("overloads");
- if (overloads.IsObject())
- {
- Vector<String> childNames = overloads.GetObject().Keys();
- for (unsigned j = 0; j < childNames.Size(); j++)
- {
- String classname = childNames.At(j);
- JSBClass* klass = GetClass(classname);
- if (!klass)
- {
- ErrorExit("Bad overload klass");
- }
- JSONValue classoverloads = overloads.Get(classname);
- Vector<String> functionNames = classoverloads.GetObject().Keys();
- for (unsigned k = 0; k < functionNames.Size(); k++)
- {
- JSONValue _sig = classoverloads.Get(functionNames[k]);
- if (!_sig.IsArray())
- {
- ErrorExit("Bad overload defintion");
- }
- JSONArray sig = _sig.GetArray();
- Vector<String> values;
- for (unsigned x = 0; x < sig.Size(); x++)
- {
- values.Push(sig[x].GetString());
- }
- JSBFunctionSignature* fo = new JSBFunctionSignature(functionNames[k], values);
- klass->AddFunctionOverride(fo);
- }
- }
- }
- }
- void JSBModule::ProcessExcludes()
- {
- // excludes
- JSONValue root = moduleJSON_->GetRoot();
- JSONValue excludes = root.Get("excludes");
- if (excludes.IsObject())
- {
- Vector<String> childNames = excludes.GetObject().Keys();
- for (unsigned j = 0; j < childNames.Size(); j++)
- {
- String classname = childNames.At(j);
- JSBClass* klass = GetClass(classname);
- if (!klass)
- {
- ErrorExit("Bad exclude klass");
- }
- JSONValue classexcludes = excludes.Get(classname);
- Vector<String> functionNames = classexcludes.GetObject().Keys();
- for (unsigned k = 0; k < functionNames.Size(); k++)
- {
- JSONValue _sig = classexcludes.Get(functionNames[k]);
- if (!_sig.IsArray())
- {
- ErrorExit("Bad exclude defintion");
- }
- JSONArray sig = _sig.GetArray();
- Vector<String> values;
- for (unsigned x = 0; x < sig.Size(); x++)
- {
- values.Push(sig[x].GetString());
- }
- JSBFunctionSignature* fe = new JSBFunctionSignature(functionNames[k], values);
- klass->AddFunctionExclude(fe);
- }
- }
- }
- }
- void JSBModule::ProcessClassExcludes()
- {
- JSONValue root = moduleJSON_->GetRoot();
- JSONValue excludes = root.Get("classExcludes");
- if (excludes.IsObject())
- {
- Vector<String> classes = excludes.GetObject().Keys();
- for (unsigned i = 0; i < classes.Size(); i++)
- {
- const String& classname = classes[i];
- if (!classExcludes_.Contains(classname))
- {
- classExcludes_[classname] = Vector<String>();
- }
- JSONArray platforms = excludes[classname].GetArray();
- for (unsigned j = 0; j < platforms.Size(); j++)
- {
- classExcludes_[classname].Push(platforms[j].GetString());
- }
- }
- }
- }
- void JSBModule::ProcessTypeScriptDecl()
- {
- // TypeScript declarations
- JSONValue root = moduleJSON_->GetRoot();
- JSONValue decl = root.Get("typescript_decl");
- if (decl.IsObject())
- {
- Vector<String> childNames = decl.GetObject().Keys();
- for (unsigned j = 0; j < childNames.Size(); j++)
- {
- String classname = childNames.At(j);
- JSBClass* klass = GetClass(classname);
- if (!klass)
- {
- ErrorExit("Bad TypeScript decl klass");
- }
- JSONArray classdecl = decl.Get(classname).GetArray();
- for (unsigned k = 0; k < classdecl.Size(); k++)
- {
- klass->AddTypeScriptDecl(classdecl[k].GetString());
- }
- }
- }
- }
- void JSBModule::ProcessHaxeDecl()
- {
- // Haxe declarations
- JSONValue root = moduleJSON_->GetRoot();
- JSONValue decl = root.Get("haxe_decl");
- if (decl.IsObject())
- {
- Vector<String> childNames = decl.GetObject().Keys();
- for (unsigned j = 0; j < childNames.Size(); j++)
- {
- String classname = childNames.At(j);
- JSBClass* klass = GetClass(classname);
- if (!klass)
- {
- ErrorExit("Bad Haxe decl class");
- }
- JSONArray classdecl = decl.Get(classname).GetArray();
- for (unsigned k = 0; k < classdecl.Size(); k++)
- {
- klass->AddHaxeDecl(classdecl[k].GetString());
- }
- }
- }
- }
- void JSBModule::ScanHeaders()
- {
- JSBind* jsbind = GetSubsystem<JSBind>();
- FileSystem* fs = GetSubsystem<FileSystem>();
- const String& sourceRoot = jsbind->GetSourceRootFolder();
- for (unsigned i = 0; i < sourceDirs_.Size(); i++)
- {
- const String& dir = sourceRoot + sourceDirs_[i] + "/";
- Vector<String> fileNames;
- fs->ScanDir(fileNames, dir, "*.h", SCAN_FILES, false);
- for (unsigned k = 0; k < fileNames.Size(); k++)
- {
- String filepath = dir + fileNames[k];
- SharedPtr<JSBHeader> header(new JSBHeader(context_, this, filepath));
- // Parse the C++ header
- header->Parse();
- headers_.Push(header);
- }
- }
- }
- JSBClass* JSBModule::GetClass(const String& name)
- {
- if (classes_.Contains(name))
- return classes_[name];
- return 0;
- }
- void JSBModule::RegisterClass(String name)
- {
- String nativeName = name;
- if (classnames_.Contains(name))
- {
- if (classRenames_.Contains(name))
- {
- name = classRenames_[name];
- }
- if (JSBPackage::GetClassAllPackages(nativeName))
- {
- ErrorExit(ToString("Class collision: %s", name.CString()));
- }
- JSBClass* cls = new JSBClass(context_, this, name, nativeName);
- if (genericClassnames_.Contains(name))
- {
- cls->SetGeneric();
- }
- classes_[nativeName] = cls;
- package_->RegisterClass(cls);
- }
- }
- void JSBModule::RegisterEnum(JSBEnum* jenum)
- {
- if (JSBPackage::GetClassAllPackages(jenum->GetName()))
- {
- ErrorExit(ToString("Enum collision: %s", jenum->GetName().CString()));
- }
- enums_[jenum->GetName()] = jenum;
- }
- JSBEnum* JSBModule::GetEnum(const String& name)
- {
- if (enums_.Contains(name))
- {
- return enums_[name];
- }
- return 0;
- }
- String JSBModule::GetClassDefineGuard(const String& name, const String& language) const
- {
- StringVector platforms;
- if (!classExcludes_.TryGetValue(name, platforms) || !platforms.Size())
- return String::EMPTY;
- Vector<String> defines;
- for (unsigned i = 0; i < platforms.Size(); i++)
- {
- String platform = platforms[i].ToLower();
- if (platform == "windows")
- {
- if (language == "csharp")
- {
- if (!defines.Contains("!ATOMIC_DESKTOP"))
- defines.Push("!ATOMIC_DESKTOP");
- }
- else
- {
- defines.Push("!defined(ATOMIC_PLATFORM_WINDOWS)");
- }
-
- }
- else if (platform == "macosx")
- {
- if (language == "csharp")
- {
- if (!defines.Contains("!ATOMIC_DESKTOP"))
- defines.Push("!ATOMIC_DESKTOP");
- }
- else
- {
- defines.Push("!defined(ATOMIC_PLATFORM_OSX)");
- }
- }
- else if (platform == "linux")
- {
- if (language == "csharp")
- {
- if (!defines.Contains("!ATOMIC_DESKTOP"))
- defines.Push("!ATOMIC_DESKTOP");
- }
- else
- {
- defines.Push("!defined(ATOMIC_PLATFORM_LINUX)");
- }
- }
- else if (platform == "android")
- {
- if (language == "csharp")
- {
- defines.Push("!ATOMIC_ANDROID");
- }
- else
- {
- defines.Push("!defined(ATOMIC_PLATFORM_ANDROID)");
- }
- }
- else if (platform == "ios")
- {
- if (language == "csharp")
- {
- defines.Push("!ATOMIC_IOS");
- }
- else
- {
- defines.Push("!defined(ATOMIC_PLATFORM_IOS)");
- }
- }
- else if (platform == "web")
- {
- if (language == "csharp")
- {
- defines.Push("!ATOMIC_WEB");
- }
- else
- {
- defines.Push("!defined(ATOMIC_PLATFORM_WEB)");
- }
- }
- else
- {
- ATOMIC_LOGERRORF("Unknown package platform: %s", platform.CString());
- }
- }
- if (!defines.Size())
- return String::EMPTY;
- String defineString = "#if " + String::Joined(defines, " && ");
- return defineString;
- }
- String JSBModule::GetModuleDefineGuard() const
- {
- // platform -> vector of modules
- const HashMap<String, Vector<String>>& platformExcludes = package_->GetModuleExcludes();
- HashMap<String, Vector<String>>::ConstIterator itr = platformExcludes.Begin();
- Vector<String> defines;
- while (itr != platformExcludes.End())
- {
- const String& platform = itr->first_;
- const Vector<String>& modules = itr->second_;
- for (unsigned i = 0; i < modules.Size(); i++)
- {
- if (modules[i].ToLower() == name_.ToLower())
- {
- if (platform.ToLower() == "windows")
- defines.Push("!defined(ATOMIC_PLATFORM_WINDOWS)");
- else if (platform.ToLower() == "macosx")
- defines.Push("!defined(ATOMIC_PLATFORM_OSX)");
- else if (platform.ToLower() == "linux")
- defines.Push("!defined(ATOMIC_PLATFORM_LINUX)");
- else if (platform.ToLower() == "android")
- defines.Push("!defined(ATOMIC_PLATFORM_ANDROID)");
- else if (platform.ToLower() == "ios")
- defines.Push("!defined(ATOMIC_PLATFORM_IOS)");
- else if (platform.ToLower() == "web")
- defines.Push("!defined(ATOMIC_PLATFORM_WEB)");
- else
- {
- ATOMIC_LOGERRORF("Unknown package platform: %s", platform.CString());
- }
- break;
- }
- }
-
- itr++;
- }
-
- if (!defines.Size())
- return String::EMPTY;
- String defineString = "#if " + String::Joined(defines, " && ");
- return defineString;
-
- }
- bool JSBModule::ContainsConstant(const String& constantName)
- {
- return constants_.Contains(constantName);
- }
- void JSBModule::RegisterConstant(const String& constantName, const String& value, unsigned type, bool isUnsigned)
- {
- // MAX_CASCADE_SPLITS is defined differently for desktop/mobile
- if (constantName == "MAX_CASCADE_SPLITS" && JSBPackage::ContainsConstantAllPackages(constantName))
- {
- return;
- }
- if (JSBPackage::ContainsConstantAllPackages(constantName))
- {
- ErrorExit(ToString("Constant collision: %s", constantName.CString()));
- }
- Constant c;
- c.type = new JSBPrimitiveType(type, isUnsigned);
- c.value = value;
- constants_[constantName] = c;
- }
- bool JSBModule::Load(const String& jsonFilename)
- {
- ATOMIC_LOGINFOF("Loading Module: %s", jsonFilename.CString());
- JSBind* jsbind = GetSubsystem<JSBind>();
- SharedPtr<File> jsonFile(new File(context_, jsonFilename));
- if (!jsonFile->IsOpen())
- {
- ATOMIC_LOGERRORF("Unable to open module json: %s", jsonFilename.CString());
- return false;
- }
- moduleJSON_ = new JSONFile(context_);
- if (!moduleJSON_->BeginLoad(*jsonFile))
- {
- ATOMIC_LOGERRORF("Unable to parse module json: %s", jsonFilename.CString());
- return false;
- }
- JSONValue root = moduleJSON_->GetRoot();
- name_ = root.Get("name").GetString();
- JSONValue requires = root.Get("requires");
- if (requires.IsArray())
- {
- for (unsigned j = 0; j < requires.GetArray().Size(); j++)
- {
- requirements_.Push(requires[j].GetString());
- }
- }
- JSONArray classes = root.Get("classes").GetArray();
- for (unsigned i = 0; i < classes.Size(); i++)
- {
- classnames_.Push(classes[i].GetString());
- }
- JSONArray classesGeneric = root.Get("classes_generic").GetArray();
- for (unsigned i = 0; i < classesGeneric.Size(); i++)
- {
- genericClassnames_.Push(classesGeneric[i].GetString());
- }
- JSONValue classes_rename = root.Get("classes_rename");
- if (classes_rename.IsObject())
- {
- Vector<String> childNames = classes_rename.GetObject().Keys();
- for (unsigned j = 0; j < childNames.Size(); j++)
- {
- String classname = childNames.At(j);
- String crename = classes_rename.Get(classname).GetString();
- classRenames_[classname] = crename;
- }
- }
- JSONValue includes = root.Get("includes");
- if (includes.IsArray())
- {
- for (unsigned j = 0; j < includes.GetArray().Size(); j++)
- {
- includes_.Push(includes.GetArray()[j].GetString());
- }
- }
- JSONValue jsmodulepreamble = root.Get("jsmodulepreamble");
- if (jsmodulepreamble.IsArray())
- {
- for (unsigned j = 0; j < jsmodulepreamble.GetArray().Size(); j++)
- {
- jsmodulePreamble_.Push(jsmodulepreamble.GetArray()[j].GetString());
- }
- }
- JSONValue sources = root.Get("sources");
- for (unsigned i = 0; i < sources.GetArray().Size(); i++)
- {
- sourceDirs_.Push(sources.GetArray()[i].GetString());
- }
- ScanHeaders();
- return true;
- }
- }
|