mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-09-21 20:06:40 +00:00
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:
@@ -0,0 +1,4 @@
|
||||
function foo()
|
||||
{
|
||||
import "include/pi.js";
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { circleArea } from "include/geometry/area.js";
|
||||
|
||||
class Circle
|
||||
{
|
||||
radius;
|
||||
|
||||
constructor(radius)
|
||||
{
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
get area()
|
||||
{
|
||||
return circleArea(this.radius);
|
||||
}
|
||||
}
|
||||
|
||||
export default Circle;
|
||||
@@ -0,0 +1,8 @@
|
||||
import RenamedCircle from "include/circle.js";
|
||||
|
||||
const area = new RenamedCircle(10).area;
|
||||
|
||||
if (area === (Math.PI * 100))
|
||||
log("Test succeeded");
|
||||
else
|
||||
throw new Error("Module Evalutation Error");
|
||||
@@ -0,0 +1,6 @@
|
||||
import importedPi from "include/pi.js";
|
||||
|
||||
export function circleArea(radius)
|
||||
{
|
||||
return importedPi * (radius * radius);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export default Math.PI;
|
||||
Reference in New Issue
Block a user