Hi All,
I wanted to share the latest feature I've been working on for QBJS to solicit some input from the community. I've implemented the ability to include "modules" from external source files into your program. This concept is similar to include from c/c++ and import from javascript. The idea here is to make it easy to create reusable libraries that can be shared between projects. Here's an example:
Import MyLib From "https://raw.githubusercontent.com/boxgaming/qbjs/main/samples/include/test.bas"
Import Maths From "https://raw.githubusercontent.com/boxgaming/qbjs/main/samples/include/maths.bas"
MyLib.DoStuff ' Call exported sub
Print MyLib.GetThings
' Call exported function
Print Maths.Factorial
(10)
Try it on QBJS-DevAny imports must be the first statements in your program. In this example we've imported functionality from two different external source files. Any methods exported from those libraries can be accessed in your program by prefixing them with the alias specified after the import statement.
Only the content specified for export will be available in the calling program. This lets us have "private" shared variables and helper functions in our included modules.
Here's what the library files look like:
test.bas
Export GetStuff
As GetThings
Export DoStuff
foo = "somthing"
GetStuff = "got the stuff"
maths.bas
Export increment
As Plus1
Export factorial
AS Factorial
increment = num + 1
if (num
=== 0 || num
=== 1) { num = 1;
}
for (var i
= num
- 1; i
>= 1; i
--) { num *= i;
}
}
factorial = num
Export statements must appear at the top of your module code. If you want to have a different name for the exported method from it's internal name you can use the "As
Alias" syntax as shown above.
Import Entire ProgramsAnother benefit of this feature is that you can import entire programs from an external source file to make it easier to share large programs. Here are a couple of examples:
QBJS Paint
Import QBJSPaint From "https://raw.githubusercontent.com/boxgaming/qbjs/main/samples/apps/paint.bas"
Try it on QBJS-DevTerry Ritchie's Excellent Flappy Bird Clone
Ported from QB64Import FlappyBird From "https://raw.githubusercontent.com/boxgaming/qbjs/main/samples/games/trfbird.bas"
Try it on QBJS-DevI'd be very interested to hear your thoughts.