Skip to content

Commit 55e915c

Browse files
committed
Add GodotJavaScriptLanguage
1 parent fdce4a7 commit 55e915c

File tree

12 files changed

+334
-173
lines changed

12 files changed

+334
-173
lines changed

bridge/jsb_class_info.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ namespace jsb
238238
void ScriptClassInfo::instantiate(const StringName& p_module_id, const v8::Local<v8::Object>& p_self)
239239
{
240240
const String source_path = internal::PathUtil::convert_javascript_path(p_module_id);
241-
const Ref<GodotJSScript> script = ResourceLoader::load(source_path);
241+
const Ref<GodotJSScriptBase> script = ResourceLoader::load(source_path);
242242
if (script.is_valid())
243243
{
244244
jsb_unused(script->can_instantiate()); // make it loaded immediately

internal/jsb_string_names.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@
66
#define jsb_string_name(name) ::jsb::internal::StringNames::get_singleton().sn_##name
77
#define jsb_literal(name) (sizeof(::jsb::internal::StringNames::sn_##name) == sizeof(StringName), #name)
88

9-
class GodotJSScriptLanguage;
9+
class GodotJSScriptLanguageBase;
1010

1111
namespace jsb::internal
1212
{
1313
class StringNames
1414
{
1515
private:
16-
friend class ::GodotJSScriptLanguage;
16+
friend class ::GodotJSScriptLanguageBase;
1717

1818
static StringNames* singleton_;
1919

2020
static void create() { singleton_ = memnew(StringNames); }
2121
static void free()
2222
{
23-
memdelete(singleton_);
24-
singleton_ = nullptr;
23+
if (singleton_) {
24+
memdelete(singleton_);
25+
singleton_ = nullptr;
26+
}
2527
}
2628

2729
// we need to ignore some names used in godot (such as XXX.name) to avoid conflicts in javascript.

register_types.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "weaver-editor/jsb_export_plugin.h"
1313
#endif
1414

15-
static GodotJSScriptLanguage* script_language_js;
1615
static Ref<ResourceFormatLoaderGodotJSScript> resource_loader;
1716
static Ref<ResourceFormatSaverGodotJSScript> resource_saver;
1817

@@ -21,29 +20,31 @@ void jsb_initialize_module(ModuleInitializationLevel p_level)
2120
if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS)
2221
{
2322
GDREGISTER_CLASS(GodotJSScript);
23+
GDREGISTER_CLASS(GodotJavaScript);
2424

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

2727
// register javascript language
28-
script_language_js = memnew(GodotJSScriptLanguage());
29-
ScriptServer::register_language(script_language_js);
28+
ScriptServer::register_language(GodotJSScriptLanguage::create_singleton());
29+
ScriptServer::register_language(GodotJavascriptLanguage::create_singleton());
3030

3131
resource_loader.instantiate();
32-
resource_loader->register_resource_extension(JSB_TYPESCRIPT_EXT, script_language_js);
33-
resource_loader->register_resource_extension(JSB_JAVASCRIPT_EXT, script_language_js);
32+
resource_loader->register_resource_extension(JSB_TYPESCRIPT_EXT, GodotJSScriptLanguage::get_singleton());
33+
resource_loader->register_resource_extension(JSB_JAVASCRIPT_EXT, GodotJavascriptLanguage::get_singleton());
3434
ResourceLoader::add_resource_format_loader(resource_loader);
3535

3636
resource_saver.instantiate();
3737
ResourceSaver::add_resource_format_saver(resource_saver);
3838

39+
3940
#ifdef TOOLS_ENABLED
4041
EditorNode::add_init_callback([]
4142
{
42-
GodotJSEditorPlugin* plugin = memnew(GodotJSEditorPlugin(script_language_js));
43+
GodotJSEditorPlugin* plugin = memnew(GodotJSEditorPlugin(GodotJSScriptLanguage::get_singleton()));
4344
EditorNode::add_editor_plugin(plugin);
4445

4546
Ref<GodotJSExportPlugin> exporter;
46-
exporter.instantiate(script_language_js->get_environment());
47+
exporter.instantiate(GodotJSScriptLanguage::get_singleton()->get_environment());
4748
EditorExport::get_singleton()->add_export_plugin(exporter);
4849

4950
plugin->set_name(jsb_typename(GodotJSEditorPlugin));
@@ -62,8 +63,10 @@ void jsb_uninitialize_module(ModuleInitializationLevel p_level)
6263
ResourceSaver::remove_resource_format_saver(resource_saver);
6364
resource_saver.unref();
6465

65-
jsb_check(script_language_js);
66-
ScriptServer::unregister_language(script_language_js);
67-
memdelete(script_language_js);
66+
ScriptServer::unregister_language(GodotJSScriptLanguage::get_singleton());
67+
GodotJSScriptLanguage::destroy_singleton();
68+
69+
ScriptServer::unregister_language(GodotJavascriptLanguage::get_singleton());
70+
GodotJavascriptLanguage::destroy_singleton();
6871
}
6972
}
Lines changed: 52 additions & 0 deletions
Loading

weaver/jsb_resource_loader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "jsb_script.h"
55

66

7-
void ResourceFormatLoaderGodotJSScript::register_resource_extension(String ext, GodotJSScriptLanguage * lang)
7+
void ResourceFormatLoaderGodotJSScript::register_resource_extension(String ext, GodotJSScriptLanguageBase * lang)
88
{
99
lang_map_.insert(ext, lang);
1010
}
@@ -13,7 +13,7 @@ Ref<Resource> ResourceFormatLoaderGodotJSScript::load(const String& p_path, cons
1313
{
1414
JSB_BENCHMARK_SCOPE(ResourceFormatLoaderGodotJSScript, load);
1515

16-
GodotJSScriptLanguage * lang = nullptr;
16+
GodotJSScriptLanguageBase * lang = nullptr;
1717
for (auto const & i: lang_map_) {
1818
if (p_path.ends_with(i.key)) {
1919
lang = i.value;
@@ -29,7 +29,7 @@ Ref<Resource> ResourceFormatLoaderGodotJSScript::load(const String& p_path, cons
2929
{
3030
//TODO a dirty but approaching solution for hot-reloading
3131
MutexLock lock(lang->mutex_);
32-
SelfList<GodotJSScript> *elem = lang->script_list_.first();
32+
SelfList<GodotJSScriptBase> *elem = lang->script_list_.first();
3333
while (elem)
3434
{
3535
if (elem->self()->get_path() == p_path)
@@ -68,7 +68,7 @@ Ref<Resource> ResourceFormatLoaderGodotJSScript::load(const String& p_path, cons
6868
JSB_LOG(VeryVerbose, "loading script resource %s on thread %s", p_path, uitos(Thread::get_caller_id()));
6969

7070
// return a skeleton script which only contains path and source code without actually loaded in `realm` since `load` may called from background threads
71-
Ref<GodotJSScript> spt = reinterpret_cast<GodotJSScript*>(lang->create_script());
71+
Ref<GodotJSScriptBase> spt = lang->create_godotjsscript();
7272
spt->attach_source(p_path);
7373
if (r_error) *r_error = OK;
7474
return spt;

weaver/jsb_resource_loader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class ResourceFormatLoaderGodotJSScript : public ResourceFormatLoader
1111
ResourceFormatLoaderGodotJSScript(ResourceFormatLoaderGodotJSScript const &) = delete;
1212
ResourceFormatLoaderGodotJSScript & operator=(ResourceFormatLoaderGodotJSScript const &) = delete;
1313

14-
HashMap<String, GodotJSScriptLanguage *> lang_map_;
14+
HashMap<String, GodotJSScriptLanguageBase *> lang_map_;
1515

1616
public:
1717
ResourceFormatLoaderGodotJSScript() { }
1818

19-
void register_resource_extension(String ext, GodotJSScriptLanguage * lang);
19+
void register_resource_extension(String ext, GodotJSScriptLanguageBase * lang);
2020

2121
virtual Ref<Resource> load(const String& p_path, const String& p_original_path = "", Error* r_error = nullptr, bool p_use_sub_threads = false, float* r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
2222
virtual void get_recognized_extensions(List<String>* p_extensions) const override;

weaver/jsb_resource_saver.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
// @seealso: gdscript.cpp ResourceFormatSaverGDScript::save
77
Error ResourceFormatSaverGodotJSScript::save(const Ref<Resource>& p_resource, const String& p_path, uint32_t p_flags)
88
{
9-
const Ref<GodotJSScript> sqscr = p_resource;
9+
const Ref<GodotJSScriptBase> sqscr = p_resource;
1010
ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER);
1111

1212
Error err;
1313
const Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
1414
if (err)
1515
{
16-
JSB_LOG(Error, "Cannot save %s file '%s'.", jsb_typename(GodotJSScript), p_path);
16+
JSB_LOG(Error, "Cannot save %s file '%s'.", jsb_typename(GodotJSScriptBase), p_path);
1717
return err;
1818
}
1919
file->store_string(sqscr->get_source_code());
@@ -33,7 +33,7 @@ Error ResourceFormatSaverGodotJSScript::save(const Ref<Resource>& p_resource, co
3333

3434
void ResourceFormatSaverGodotJSScript::get_recognized_extensions(const Ref<Resource>& p_resource, List<String>* p_extensions) const
3535
{
36-
if (Object::cast_to<GodotJSScript>(*p_resource))
36+
if (Object::cast_to<GodotJSScriptBase>(*p_resource))
3737
{
3838
p_extensions->push_back(JSB_TYPESCRIPT_EXT);
3939
p_extensions->push_back(JSB_JAVASCRIPT_EXT);
@@ -42,5 +42,5 @@ void ResourceFormatSaverGodotJSScript::get_recognized_extensions(const Ref<Resou
4242

4343
bool ResourceFormatSaverGodotJSScript::recognize(const Ref<Resource>& p_resource) const
4444
{
45-
return Object::cast_to<GodotJSScript>(*p_resource) != nullptr;
45+
return Object::cast_to<GodotJSScriptBase>(*p_resource) != nullptr;
4646
}

0 commit comments

Comments
 (0)