1
Programs / QBJS - Import code from external .bas files
« on: April 15, 2022, 04:43:54 pm »
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:
Try it on QBJS-Dev
Any 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
maths.bas
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 Programs
Another 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
Try it on QBJS-Dev
Terry Ritchie's Excellent Flappy Bird Clone
Ported from QB64
Try it on QBJS-Dev
I'd be very interested to hear your thoughts.
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:
Code: QB64: [Select]
- 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
Try it on QBJS-Dev
Any 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
Code: QB64: [Select]
maths.bas
Code: QB64: [Select]
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 Programs
Another 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
Code: QB64: [Select]
- Import QBJSPaint From "https://raw.githubusercontent.com/boxgaming/qbjs/main/samples/apps/paint.bas"
Terry Ritchie's Excellent Flappy Bird Clone
Ported from QB64
Code: QB64: [Select]
- Import FlappyBird From "https://raw.githubusercontent.com/boxgaming/qbjs/main/samples/games/trfbird.bas"
I'd be very interested to hear your thoughts.