Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ __MACOSX
# auto-generated sources
/jsb.gen.h
/jsb_project_preset.gen.cpp
/weaver-editor/templates/templates.gen.h
/weaver-editor/templates/templates.ts.gen.h
/weaver-editor/templates/templates.js.gen.h

.vscode
2 changes: 1 addition & 1 deletion bridge/jsb_class_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ namespace jsb
void ScriptClassInfo::instantiate(const StringName& p_module_id, const v8::Local<v8::Object>& p_self)
{
const String source_path = internal::PathUtil::convert_javascript_path(p_module_id);
const Ref<GodotJSScript> script = ResourceLoader::load(source_path);
const Ref<GodotJSScriptBase> script = ResourceLoader::load(source_path);
if (script.is_valid())
{
jsb_unused(script->can_instantiate()); // make it loaded immediately
Expand Down
10 changes: 6 additions & 4 deletions internal/jsb_string_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
#define jsb_string_name(name) ::jsb::internal::StringNames::get_singleton().sn_##name
#define jsb_literal(name) (sizeof(::jsb::internal::StringNames::sn_##name) == sizeof(StringName), #name)

class GodotJSScriptLanguage;
class GodotJSScriptLanguageBase;

namespace jsb::internal
{
class StringNames
{
private:
friend class ::GodotJSScriptLanguage;
friend class ::GodotJSScriptLanguageBase;

static StringNames* singleton_;

static void create() { singleton_ = memnew(StringNames); }
static void free()
{
memdelete(singleton_);
singleton_ = nullptr;
if (singleton_) {
memdelete(singleton_);
singleton_ = nullptr;
}
}

// we need to ignore some names used in godot (such as XXX.name) to avoid conflicts in javascript.
Expand Down
44 changes: 26 additions & 18 deletions register_types.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "register_types.h"

#include "weaver/jsb_script_language.h"
#include "weaver/jsb_script_language_ts.h"
#include "weaver/jsb_script_language_js.h"
#include "weaver/jsb_script.h"
#include "weaver/jsb_resource_loader.h"
#include "weaver/jsb_resource_saver.h"
Expand All @@ -12,35 +14,39 @@
#include "weaver-editor/jsb_export_plugin.h"
#endif

static Ref<ResourceFormatLoaderGodotJSScript> resource_loader_js;
static Ref<ResourceFormatSaverGodotJSScript> resource_saver_js;
static Ref<ResourceFormatLoaderGodotJSScript> resource_loader;
static Ref<ResourceFormatSaverGodotJSScript> resource_saver;

void jsb_initialize_module(ModuleInitializationLevel p_level)
{
if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS)
{
GDREGISTER_CLASS(GodotJSScript);
GDREGISTER_CLASS(GodotJavaScript);

jsb::impl::GlobalInitialize::init();

// register javascript language
GodotJSScriptLanguage* script_language_js = memnew(GodotJSScriptLanguage());
ScriptServer::register_language(script_language_js);
ScriptServer::register_language(GodotJSScriptLanguage::create_singleton());
ScriptServer::register_language(GodotJavascriptLanguage::create_singleton());

resource_loader_js.instantiate();
ResourceLoader::add_resource_format_loader(resource_loader_js);
resource_loader.instantiate();
resource_loader->register_resource_extension(JSB_TYPESCRIPT_EXT, GodotJSScriptLanguage::get_singleton());
resource_loader->register_resource_extension(JSB_JAVASCRIPT_EXT, GodotJavascriptLanguage::get_singleton());
ResourceLoader::add_resource_format_loader(resource_loader);

resource_saver.instantiate();
ResourceSaver::add_resource_format_saver(resource_saver);

resource_saver_js.instantiate();
ResourceSaver::add_resource_format_saver(resource_saver_js);

#ifdef TOOLS_ENABLED
EditorNode::add_init_callback([]
{
GodotJSEditorPlugin* plugin = memnew(GodotJSEditorPlugin);
GodotJSEditorPlugin* plugin = memnew(GodotJSEditorPlugin(GodotJSScriptLanguage::get_singleton()));
EditorNode::add_editor_plugin(plugin);

Ref<GodotJSExportPlugin> exporter;
exporter.instantiate();
exporter.instantiate(GodotJSScriptLanguage::get_singleton()->get_environment());
EditorExport::get_singleton()->add_export_plugin(exporter);

plugin->set_name(jsb_typename(GodotJSEditorPlugin));
Expand All @@ -53,15 +59,17 @@ void jsb_uninitialize_module(ModuleInitializationLevel p_level)
{
if (p_level == MODULE_INITIALIZATION_LEVEL_CORE)
{
ResourceLoader::remove_resource_format_loader(resource_loader_js);
resource_loader_js.unref();
ResourceLoader::remove_resource_format_loader(resource_loader);
resource_loader.unref();

ResourceSaver::remove_resource_format_saver(resource_saver);
resource_saver.unref();

ScriptServer::unregister_language(GodotJSScriptLanguage::get_singleton());
GodotJSScriptLanguage::destroy_singleton();

ResourceSaver::remove_resource_format_saver(resource_saver_js);
resource_saver_js.unref();
ScriptServer::unregister_language(GodotJavascriptLanguage::get_singleton());
GodotJavascriptLanguage::destroy_singleton();

GodotJSScriptLanguage *script_language_js = GodotJSScriptLanguage::get_singleton();
jsb_check(script_language_js);
ScriptServer::unregister_language(script_language_js);
memdelete(script_language_js);
}
}
52 changes: 52 additions & 0 deletions weaver-editor/icons/GodotJavaScript.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions weaver-editor/jsb_editor_pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../internal/jsb_settings.h"

#include "../weaver/jsb_script_language.h"
#include "../weaver/jsb_script_language_ts.h"

#if GODOT_4_3_OR_NEWER
# include "editor/plugins/editor_plugin.h"
Expand Down
10 changes: 5 additions & 5 deletions weaver-editor/jsb_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void GodotJSEditorPlugin::_notification(int p_what)
switch (p_what)
{
case NOTIFICATION_APPLICATION_FOCUS_IN:
if (GodotJSScriptLanguage* lang = GodotJSScriptLanguage::get_singleton())
if (GodotJSScriptLanguage* lang = lang_)
{
lang->scan_external_changes();
}
Expand All @@ -61,7 +61,7 @@ void GodotJSEditorPlugin::_on_menu_pressed(int p_what)
}
}

GodotJSEditorPlugin::GodotJSEditorPlugin()
GodotJSEditorPlugin::GodotJSEditorPlugin(GodotJSScriptLanguage * lang) : lang_(lang)
{
// jsb::internal::Settings::on_editor_init();
PopupMenu *menu = memnew(PopupMenu);
Expand Down Expand Up @@ -369,7 +369,7 @@ void GodotJSEditorPlugin::generate_godot_dts()
install_files(filter_files(editor_plugin->install_files_, jsb::weaver::CH_D_TS));
}

GodotJSScriptLanguage* lang = GodotJSScriptLanguage::get_singleton();
GodotJSScriptLanguage * lang = get_singleton()->lang_;
jsb_check(lang);
Error err;
const String code = jsb_format(R"--((function(){const mod = require("jsb.editor.codegen"); (new mod.default("%s")).emit();})())--", "./" JSB_TYPE_ROOT);
Expand All @@ -382,7 +382,7 @@ void GodotJSEditorPlugin::generate_godot_dts()

void GodotJSEditorPlugin::load_editor_entry_module()
{
GodotJSScriptLanguage* lang = GodotJSScriptLanguage::get_singleton();
GodotJSScriptLanguage * lang = get_singleton()->lang_;
jsb_check(lang);
const Error err = lang->get_environment()->load("jsb.editor.main");
ERR_FAIL_COND_MSG(err != OK, "failed to evaluate jsb.editor.main");
Expand Down Expand Up @@ -458,7 +458,7 @@ GodotJSEditorPlugin* GodotJSEditorPlugin::get_singleton()

void GodotJSEditorPlugin::ensure_tsc_installed()
{
GodotJSScriptLanguage* lang = GodotJSScriptLanguage::get_singleton();
GodotJSScriptLanguage * lang = get_singleton()->lang_;
jsb_check(lang);

Error err;
Expand Down
6 changes: 5 additions & 1 deletion weaver-editor/jsb_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class GodotJSEditorPlugin : public EditorPlugin

std::shared_ptr<jsb::internal::Process> tsc_;

GodotJSScriptLanguage * lang_;

protected:
static void _bind_methods();

Expand All @@ -70,9 +72,11 @@ class GodotJSEditorPlugin : public EditorPlugin
static bool delete_file(const String& p_file);

public:
GodotJSEditorPlugin();
GodotJSEditorPlugin(GodotJSScriptLanguage * lang_);
virtual ~GodotJSEditorPlugin() override;

GodotJSScriptLanguage * get_lang() const { return lang_; }

void start_tsc_watch();
bool is_tsc_watching();
void kill_tsc();
Expand Down
3 changes: 1 addition & 2 deletions weaver-editor/jsb_export_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

#define JSB_EXPORTER_LOG(Severity, Format, ...) JSB_LOG_IMPL(JSExporter, Severity, Format, ##__VA_ARGS__)

GodotJSExportPlugin::GodotJSExportPlugin() : super()
GodotJSExportPlugin::GodotJSExportPlugin(std::shared_ptr<jsb::Environment> & env) : super(), env_(env)
{
// explicitly ignored files (not used by runtime)
ignored_paths_.insert("res://jsconfig.json");
ignored_paths_.insert("res://tsconfig.json");
ignored_paths_.insert("res://package.json");
ignored_paths_.insert("res://package-lock.json");
env_ = GodotJSScriptLanguage::get_singleton()->get_environment();
jsb_check(env_);
}

Expand Down
2 changes: 1 addition & 1 deletion weaver-editor/jsb_export_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GodotJSExportPlugin: public EditorExportPlugin
GDCLASS(GodotJSExportPlugin, EditorExportPlugin)

public:
GodotJSExportPlugin();
GodotJSExportPlugin(std::shared_ptr<jsb::Environment> & env);
virtual String get_name() const override;
virtual bool supports_platform(const Ref<EditorExportPlatform>& p_export_platform) const override;

Expand Down
4 changes: 2 additions & 2 deletions weaver-editor/jsb_repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void GodotJSREPL::check_install()

void GodotJSREPL::_gc_pressed()
{
GodotJSScriptLanguage* lang = GodotJSScriptLanguage::get_singleton();
GodotJSScriptLanguage* lang = GodotJSEditorPlugin::get_singleton()->get_lang();
jsb_check(lang);
lang->get_environment()->gc();
add_line("Explicit GC requested");
Expand Down Expand Up @@ -301,7 +301,7 @@ void GodotJSREPL::_input_submitted(const String &p_text)

jsb::JSValueMove GodotJSREPL::eval_source(const String& p_code)
{
GodotJSScriptLanguage* lang = GodotJSScriptLanguage::get_singleton();
GodotJSScriptLanguage* lang = GodotJSEditorPlugin::get_singleton()->get_lang();
jsb_check(lang);
Error err;
return lang->eval_source(p_code, err);
Expand Down
3 changes: 2 additions & 1 deletion weaver-editor/jsb_statistics_viewer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "jsb_statistics_viewer.h"
#include "jsb_editor_pch.h"
#include "jsb_editor_plugin.h"

GodotJSStatisticsViewer::GodotJSStatisticsViewer()
{
Expand Down Expand Up @@ -41,7 +42,7 @@ void GodotJSStatisticsViewer::activate(bool p_active)

void GodotJSStatisticsViewer::on_timer()
{
const GodotJSScriptLanguage* lang = GodotJSScriptLanguage::get_singleton();
const GodotJSScriptLanguage* lang = GodotJSEditorPlugin::get_singleton()->get_lang();
if (!lang) return;
const std::shared_ptr<jsb::Environment> env = lang->get_environment();
if (!env) return;
Expand Down
9 changes: 4 additions & 5 deletions weaver-editor/templates/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ else:
src_suffix = ".ts",
)

language = "ts" if env["use_typescript"] else "js"
print("generate templates for", language)

# Template files
# the suffix should be `.ts`, but `editor/template_builders.py` hardcoded the delimiter with `.cs`
templates_sources = Glob(f"*/*.{language}.cs")
templates_ts_sources = Glob(f"*/*.ts.cs")
env.Alias("editor_template_ts", [env.MakeGodotJSTemplateBuilder("templates.ts.gen.h", templates_ts_sources)])

env.Alias("editor_template_ts", [env.MakeGodotJSTemplateBuilder("templates.gen.h", templates_sources)])
templates_js_sources = Glob(f"*/*.js.cs")
env.Alias("editor_template_js", [env.MakeGodotJSTemplateBuilder("templates.js.gen.h", templates_js_sources)])
Loading