Example #1
0
void JSBModule::WriteModulePreInit(String& source)
{
    source.AppendWithFormat("\nvoid jsb_preinit_%s (JSVM* vm)\n{\n\njsb_declare_classes(vm);\n", name_.ToLower().CString());

    // register enums and constants
    source += "// enums and constants\n";
    source += "duk_context* ctx = vm->GetJSContext();\n";
    source += "duk_get_global_string(ctx, \"Atomic\");\n";

    source += "// enums\n";
    for (unsigned i = 0; i < enums_.Size(); i++)
    {
        JSBEnum* jenum = enums_[i];
        for (unsigned k = 0; k < jenum->values_.Size(); k++)
        {
            source.AppendWithFormat("duk_push_number(ctx, (double) %s);\n", jenum->values_.At(k).CString());
            source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", jenum->values_.At(k).CString());
        }
    }
    source += "// constants\n";

    for (unsigned i = 0; i < constants_.Size(); i++)
    {
        source.AppendWithFormat("duk_push_number(ctx, (double) %s);\n", constants_.At(i).CString());
        source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", constants_.At(i).CString());
    }

    source += "duk_pop(ctx);\n";
    source += "// end enums and constants\n";

    source += "\n}\n";

}
void ResourceOps::HandleNewFolder(const String& resourcePath, bool reportError)
{
    Editor* editor = GetSubsystem<Editor>();
    FileSystem* fs = GetSubsystem<FileSystem>();

    if (fs->DirExists(resourcePath) || fs->FileExists(resourcePath))
    {
        if (reportError)
        {
            String errorMsg;
            errorMsg.AppendWithFormat("Already exists:\n\n %s", resourcePath.CString());
            editor->PostModalError("New Folder Error", errorMsg);
        }

        return;
    }

    if (!fs->CreateDir(resourcePath))
    {
        if (reportError)
        {
            String errorMsg;
            errorMsg.AppendWithFormat("Could not create:\n\n %s", resourcePath.CString());
            editor->PostModalError("New Folder Error", errorMsg);
        }

        return;
    }

    // file watcher doesn't currently handle subdir
    GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();

}
void JSClassWriter::GenerateNonStaticFunctionsSource(String& source, String& packageName)
{
    source.AppendWithFormat("js_class_get_prototype(ctx, \"%s\", \"%s\");\n", packageName.CString(), klass_->GetName().CString());

    for (unsigned i = 0; i < klass_->functions_.Size(); i++)
    {
        JSBFunction* function = klass_->functions_.At(i);

        if (function->Skip())
            continue;

        if (function->IsConstructor() || function->IsDestructor())
            continue;

        if (function->IsStatic())
            continue;

        if (function->FirstDefaultParameter() != -1)
        {
            source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, DUK_VARARGS);\n", klass_->GetName().CString(), function->GetName().CString());
        }
        else
        {
            source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, %i);\n", klass_->GetName().CString(), function->GetName().CString(), (int)function->GetParameters().Size());
        }

        String scriptName = function->GetName();
        scriptName[0] = tolower(scriptName[0]);
        source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", scriptName.CString());

    }
    source.Append("duk_pop(ctx);\n");
}
Example #4
0
void JSBModule::WriteClassDeclaration(String& source)
{

    source += "static void jsb_declare_classes(JSVM* vm)\n{\n";

    source += "duk_context* ctx = vm->GetJSContext();\n";

    for (unsigned i = 0; i < classes_.Size(); i++)
    {
        JSBClass* klass = classes_.At(i);

        if (klass->isNumberArray())
            continue;

        source.AppendWithFormat("   js_class_declare(vm, \"%s\", jsb_constructor_%s);\n", klass->GetName().CString(), klass->GetName().CString());

        if (klass->hasProperties())
        {
            source.AppendWithFormat("js_class_push_propertyobject(vm, \"%s\");\n", klass->GetName().CString());


            Vector<String> pnames;
            klass->GetPropertyNames(pnames);

            for (unsigned j = 0; j < pnames.Size(); j++)
            {
                JSBProperty* prop = klass->GetProperty(pnames[j]);

                source.Append("duk_push_object(ctx);\n");

                if (prop->getter_ && !prop->getter_->Skip())
                {
                    source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 0);\n",
                                            klass->GetName().CString(), prop->getter_->name_.CString());
                    source.Append("duk_put_prop_string(ctx, -2, \"get\");\n");
                }
                if (prop->setter_ && !prop->setter_->Skip())
                {
                    source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 1);\n",
                                            klass->GetName().CString(), prop->setter_->name_.CString());
                    source.Append("duk_put_prop_string(ctx, -2, \"set\");\n");
                }

                pnames[j][0] = tolower(pnames[j][0]);
                source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", pnames[j].CString());

            }

            source.Append("duk_pop(ctx);\n");

        }
    }

    source += "\n}\n\n";

}
bool ResourceOps::CheckCreate2DLevel(const String& resourcePath, const String& resourceName, bool reportError)
{

    Editor* editor = GetSubsystem<Editor>();
    Project* project = editor->GetProject();

    String fullpath = resourcePath + resourceName;
    if (!resourceName.EndsWith(".tmx"))
        fullpath += ".tmx";

    FileSystem* fs = GetSubsystem<FileSystem>();

    if (fs->FileExists(fullpath))
    {
        if (reportError)
        {
            String errorMsg;
            errorMsg.AppendWithFormat("The level:\n\n%s\n\nalready exists", fullpath.CString());
            editor->PostModalError("Create 2D Level Error", errorMsg);
        }

        return false;
    }

    return true;

}
void ResourceOps::HandleResourceDelete(const String& resourcePath, bool reportError)
{
    Editor* editor = GetSubsystem<Editor>();
    FileSystem* fs = GetSubsystem<FileSystem>();

    if (fs->DirExists(resourcePath))
    {
        fs->RemoveDir(resourcePath, true);

        GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();

        return;
    }
    else if (fs->FileExists(resourcePath))
    {
        if (!fs->Delete(resourcePath))
        {
            if (reportError)
            {
                String errorMsg;
                errorMsg.AppendWithFormat("Unable to delete:\n\n %s", resourcePath.CString());
                editor->PostModalError("Delete Resource Error", errorMsg);
            }

            return;
        }

        GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();

        return;
    }
    else
    {
        if (reportError)
        {
            String errorMsg;
            errorMsg.AppendWithFormat("Unable to find:\n\n %s", resourcePath.CString());
            editor->PostModalError("Delete Resource Error", errorMsg);
        }

        return;
    }

}
void CSClassWriter::GenerateNativeSource(String& sourceOut)
{
    String source = "";

    if (klass_->IsNumberArray())
        return;

    JSBPackage* package = klass_->GetPackage();

    if (!klass_->IsInterface())
    {
        source.AppendWithFormat("ATOMIC_EXPORT_API ClassID csb_%s_%s_GetClassIDStatic()\n{\n", package->GetName().CString(), klass_->GetName().CString());
        source.AppendWithFormat("   return %s::GetClassIDStatic();\n}\n\n", klass_->GetNativeName().CString());
    }

    WriteNativeFunctions(source);

    sourceOut += source;
}
void JSPackageWriter::WriteProtoTypeRecursive(String &source, JSBClass* klass,  Vector<JSBClass*>& written)
{
    if (written.Contains(klass))
        return;

    if (klass->GetModule()->GetDotNetModule())
        return;

    PODVector<JSBClass*>& baseClasses = klass->GetBaseClasses();

    Vector<JSBClass*>::Iterator itr = baseClasses.End() - 1 ;

    while (itr != baseClasses.Begin() - 1)
    {
        WriteProtoTypeRecursive(source, (*itr), written);
        itr--;
    }

    JSBClass* base = baseClasses.Size() ? baseClasses[0] : NULL;

    if (!klass->IsNumberArray() && klass->GetPackage() == package_)
    {
        JSBModule* module = klass->GetModule();

        String moduleGuard = module->GetModuleDefineGuard();

        if (moduleGuard.Length())
        {
            source += ToString("\n%s\n", moduleGuard.CString());
        }

        if (module->Requires("3D"))
            source += "\n#ifdef ATOMIC_3D\n";

        String packageName =  klass->GetModule()->GetPackage()->GetName();
        String basePackage =  base ? base->GetModule()->GetPackage()->GetName() : "";

        source.AppendWithFormat("   js_setup_prototype(vm, \"%s\", \"%s\", \"%s\", \"%s\", %s);\n",
                                packageName.CString(), klass->GetName().CString(),
                                base ? basePackage.CString() : "", base ? base->GetName().CString() : "",
                                klass->HasProperties() ? "true" : "false");

        if (module->Requires("3D"))
            source += "#endif\n\n";

        if (moduleGuard.Length())
        {
            source += ToString("\n#endif\n", moduleGuard.CString());
        }

    }

    written.Push(klass);

}
Example #9
0
void JSBModule::WriteIncludes(String& source)
{

    Vector<JSBHeader*> allheaders;

    for (unsigned i = 0; i < enums_.Size(); i++)
    {
        allheaders.Push(enums_.At(i)->header_);
    }

    for (unsigned i = 0; i < classes_.Size(); i++)
    {
        allheaders.Push(classes_.At(i)->GetHeader());
    }

    Vector<JSBHeader*> included;
    for (unsigned i = 0; i < allheaders.Size(); i++)
    {
        JSBHeader* header = allheaders.At(i);

        if (included.Contains(header))
            continue;

        String headerPath = GetPath(header->filepath_);

		String headerfile = GetFileNameAndExtension(header->filepath_);

        headerPath.Replace(JSBind::ROOT_FOLDER + "/Source/Atomic/", "Atomic/");

        source.AppendWithFormat("#include <%s%s>\n", headerPath.CString(), headerfile.CString());

        included.Push(header);
    }

    for (unsigned i = 0; i < includes_.Size(); i++)
    {
        if (includes_[i].StartsWith("<"))
            source.AppendWithFormat("#include %s\n", includes_[i].CString());
        else
            source.AppendWithFormat("#include \"%s\"\n", includes_[i].CString());
    }
}
Example #10
0
bool ResourceOps::CheckCreateScript(const String& resourcePath, const String& resourceName, bool reportError)
{

    Editor* editor = GetSubsystem<Editor>();
    Project* project = editor->GetProject();

    String fullpath = resourcePath + resourceName;
    if (!resourceName.EndsWith(".js"))
        fullpath += ".js";

    FileSystem* fs = GetSubsystem<FileSystem>();

    if (fs->FileExists(fullpath))
    {
        if (reportError)
        {
            String errorMsg;
            errorMsg.AppendWithFormat("The script:\n\n%s\n\nalready exists", fullpath.CString());
            editor->PostModalError("Create Script Error", errorMsg);
        }

        return false;
    }

    if (!project->IsScriptsDirOrFile(resourcePath))
    {
        if (reportError)
        {
            String errorMsg;
            errorMsg.AppendWithFormat("Scripts must reside in or in a subfolder of the Scripts folder");
            editor->PostModalError("Create Script Error", errorMsg);
        }

        return false;
    }

    return true;

}
Example #11
0
void JSBModule::WriteClassDefine(String& source)
{

    source += "static void jsb_init_classes(JSVM* vm)\n{\n";

    for (unsigned i = 0; i < classes_.Size(); i++)
    {
        JSBClass* klass = classes_.At(i);

        if (klass->isNumberArray())
            continue;

        source.AppendWithFormat("   jsb_class_define_%s(vm);\n", klass->GetName().CString());
    }

    source += "\n}\n\n";

}
void IssuesWidget::UpdateIssues()
{
    AEJavascript* aejs = GetSubsystem<AEJavascript>();
    const Vector<JSError>& errors = aejs->GetJSErrors();

    issueList_->DeleteAllItems();

    for (unsigned i = 0; i < errors.Size(); i++)
    {
        const JSError& error = errors[i];

        String errorString;
        String filename = GetFileNameAndExtension(error.fullpath);

        errorString.AppendWithFormat("%s - %s - Line: %i Column: %i",
                                     filename.CString(), error.message.CString(), error.line, error.column);

        issueList_->AddItem(errorString.CString(), NULL, TBID(i));
    }

}
Example #13
0
void ResourceOps::HandleCreate2DLevel(const String& resourcePath, const String& resourceName,
                                        bool navigateToResource, bool reportError)
{
    Editor* editor = GetSubsystem<Editor>();

    if (!CheckCreate2DLevel(resourcePath, resourceName, reportError))
        return;

    ResourceCache* cache = GetSubsystem<ResourceCache>();

    SharedPtr<File> srcFile = cache->GetFile("ClockworkEditor/templates/template_empty.tmx");
    if (srcFile.Null() || !srcFile->IsOpen())
    {
        editor->PostModalError("Create Script Error", "Could not open module template");
        return;
    }

    String fullpath = resourcePath + resourceName;
    if (!resourceName.EndsWith(".tmx"))
        fullpath += ".tmx";

    if (!CopyFile(srcFile, fullpath))
    {
        String errorMsg;
        errorMsg.AppendWithFormat("Error copying template:\n\n%s\n\nto:\n\n%s",
                                  "ClockworkEditor/template_empty.tmx", fullpath.CString());
        editor->PostModalError("Create 2D Level Error", errorMsg);
        return;
    }

    if (navigateToResource)
    {
        //ResourceFrame* rframe = GetSubsystem<MainFrame>()->GetResourceFrame();
        //rframe->EditResource(fullpath);
    }

    GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();

}
void JSClassWriter::GenerateSource(String& sourceOut)
{
    String source = "";

    if (klass_->IsNumberArray())
        return;

    WriteFunctions(source);

    String packageName = klass_->GetModule()->GetPackage()->GetName();

    source.AppendWithFormat("static void jsb_class_define_%s(JSVM* vm)\n{\n", klass_->GetName().CString());
    source.Append("duk_context* ctx = vm->GetJSContext();\n");

    GenerateStaticFunctionsSource(source, packageName);

    GenerateNonStaticFunctionsSource(source, packageName);

    source.Append("}\n");

    sourceOut += source;

}
void JSPackageWriter::GenerateSource()
{
    String source = "// This file was autogenerated by JSBind, changes will be lost\n\n";

    String defineGuard = package_->GetPlatformDefineGuard();

    if (defineGuard.Length())
    {
        source += ToString("%s\n\n", defineGuard.CString());        
    }

    source += "#include <Duktape/duktape.h>\n";
    source += "#include <AtomicJS/Javascript/JSVM.h>\n";
    source += "#include <AtomicJS/Javascript/JSAPI.h>\n";

    source += "\n\nnamespace Atomic\n{\n";

    String packageLower = package_->GetName().ToLower();

    for (unsigned i = 0; i < package_->modules_.Size(); i++)
    {
        JSBModule* module = package_->modules_.At(i);

        if (module->GetDotNetModule())
            continue;

        String moduleGuard = module->GetModuleDefineGuard();

        if (moduleGuard.Length())
        {
            source += ToString("\n%s\n", moduleGuard.CString());
        }

        String moduleLower = module->GetName().ToLower();

        source.AppendWithFormat("\nextern void jsb_package_%s_preinit_%s (JSVM* vm);", packageLower.CString(), moduleLower.CString());
        source.AppendWithFormat("\nextern void jsb_package_%s_init_%s (JSVM* vm);", packageLower.CString(), moduleLower.CString());

        if (moduleGuard.Length())
        {
            source += ToString("\n#endif\n", moduleGuard.CString());
        }

    }

    source += "\n\nstatic void jsb_modules_setup_prototypes(JSVM* vm)\n{\n";

    source += "   // It is important that these are in order so the prototypes are created properly\n";
    source += "   // This isn't trivial as modules can have dependencies, so do it here\n\n";

    WriteProtoTypeSetup(source);

    source += "\n}\n";

    source.AppendWithFormat("\n\nstatic void jsb_package_%s_preinit(JSVM* vm)\n{", packageLower.CString());


    source.Append("\n    // Create the global package object\n");
    source.Append("    duk_context* ctx = vm->GetJSContext();\n");
    source.Append("    duk_push_object(ctx);\n");
    source.AppendWithFormat("    duk_put_global_string(ctx, \"%s\");\n", package_->GetName().CString());

    for (unsigned i = 0; i < package_->modules_.Size(); i++)
    {
        JSBModule* module = package_->modules_.At(i);

        if (module->GetDotNetModule())
            continue;

        String moduleGuard = module->GetModuleDefineGuard();

        if (moduleGuard.Length())
        {
            source += ToString("\n%s\n", moduleGuard.CString());
        }

        if (module->Requires("3D"))
            source += "\n#ifdef ATOMIC_3D";

        String moduleLower = module->GetName().ToLower();

        source.AppendWithFormat("\n   jsb_package_%s_preinit_%s(vm);", packageLower.CString(), moduleLower.CString());

        if (module->Requires("3D"))
            source += "\n#endif //ATOMIC_3D\n";

        if (moduleGuard.Length())
        {
            source += ToString("\n#endif\n", moduleGuard.CString());
        }

    }

    source += "\n}\n\n";

    source.AppendWithFormat("\n\nvoid jsb_package_%s_init(JSVM* vm)\n{", packageLower.CString());

    source.AppendWithFormat("\n\n   jsb_package_%s_preinit(vm);\n", packageLower.CString());

    source += "\n\n   jsb_modules_setup_prototypes(vm);\n";

    for (unsigned i = 0; i < package_->modules_.Size(); i++)
    {
        JSBModule* module = package_->modules_.At(i);

        if (module->GetDotNetModule())
            continue;

        String moduleLower = module->GetName().ToLower();

        String moduleGuard = module->GetModuleDefineGuard();

        if (moduleGuard.Length())
        {
            source += ToString("\n%s\n", moduleGuard.CString());
        }

        if (module->Requires("3D"))
            source += "\n#ifdef ATOMIC_3D";

        source.AppendWithFormat("\n   jsb_package_%s_init_%s(vm);", packageLower.CString(), moduleLower.CString());
        
        if (module->Requires("3D"))
        {
            source += "\n#endif //ATOMIC_3D\n";
        }

        if (moduleGuard.Length())
        {
            source += ToString("\n#endif\n", moduleGuard.CString());
        }

    }

    source += "\n}\n\n";

    // end Atomic namespace
    source += "\n}\n";

    if (defineGuard.Length())
    {
        source += "\n#endif\n";
    }

    JSBind* jsbind = package_->GetSubsystem<JSBind>();

    String filepath = jsbind->GetDestNativeFolder() + "/JSPackage" + package_->name_ + ".cpp";

    File file(package_->GetContext());
    file.Open(filepath, FILE_WRITE);
    file.Write(source.CString(), source.Length());
    file.Close();

    for (unsigned i = 0; i < package_->modules_.Size(); i++)
    {
        if (package_->modules_[i]->GetDotNetModule())
            continue;

        JSModuleWriter writer(package_->modules_[i]);
        writer.GenerateSource();
    }

}
Example #16
0
void JSBModule::WriteModuleInit(String& source)
{
    source.AppendWithFormat("\nvoid jsb_init_%s (JSVM* vm)\n{\n\n    jsb_init_classes(vm);\n\n}\n\n", name_.ToLower().CString());
}