1
0
forked from mirrors/0ad

Support JavaScript modules

- With modules JavaScript code can be split up into multiple files. We
	already implemented such a mechanism (`Engine.LoadLibrary`) in
	multiple parts of the engine. The advantage of using modules is
	that it's standart (JS-devs are familiar with it) and it doesn't
	has to be implemented multiple times.
	Note that `Engine.LoadLibrary` loads all files in a directory
	while the new `import` only loads one file.

- With modules seemingly global variables are local to that
	script/module. We already implemented such a mechanism
	(`ScriptInterface::LoadScript`).
This commit is contained in:
phosit
2025-01-15 20:38:37 +01:00
committed by phosit
parent 475053ea7c
commit c6d42ebbd5
13 changed files with 360 additions and 8 deletions
+8 -1
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2024 Wildfire Games.
/* Copyright (C) 2025 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -25,6 +25,7 @@
#include "ps/Filesystem.h"
#include "ps/Profile.h"
#include "scriptinterface/FunctionWrapper.h"
#include "scriptinterface/ModuleLoader.h"
#include "scriptinterface/Object.h"
#include "scriptinterface/ScriptContext.h"
#include "scriptinterface/ScriptExtraHeaders.h"
@@ -67,6 +68,7 @@ struct ScriptInterface_impl
public:
boost::rand48* m_rng;
JS::PersistentRootedObject m_nativeScope; // native function scope object
Script::ModuleLoader m_ModuleLoader;
};
/**
@@ -467,6 +469,11 @@ ScriptContext& ScriptInterface::GetContext() const
return m->m_context;
}
Script::ModuleLoader& ScriptInterface::GetModuleLoader() const
{
return m->m_ModuleLoader;
}
void ScriptInterface::CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const
{
ScriptRequest rq(this);