Author Topic: QBJS - Import code from external .bas files  (Read 2693 times)

0 Members and 1 Guest are viewing this topic.

Offline dbox

  • Newbie
  • Posts: 80
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:

Code: QB64: [Select]
  1. Import MyLib From ""
  2. Import Maths From ""
  4. MyLib.DoStuff          ' Call exported sub
  5. Print MyLib.GetThings  ' Call exported function
  7. Print Maths.Factorial(10)
  8. Print Maths.Plus1(11)

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:

Code: QB64: [Select]
  1. Export GetStuff As GetThings
  2. Export DoStuff
  4. Dim foo
  5. foo = "somthing"
  7. Function GetStuff
  8.     GetStuff = "got the stuff"
  10. Sub DoStuff
  11.     Print "do the stuff"

Code: QB64: [Select]
  1. Export increment As Plus1
  2. Export factorial AS Factorial
  4. Function increment (num)
  5.     increment = num + 1
  7. Function factorial (num)
  8.     Dim res
  9.     $If Javascript Then
  10.         if (num === 0 || num === 1) {
  11.             num = 1;
  12.         }
  13.         else {
  14.             for (var i = num - 1; i >= 1; i--) {
  15.                 num *= i;
  16.             }
  17.        }
  18.     $End If
  19.     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 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]
  1. Import QBJSPaint From ""
Try it on QBJS-Dev

Terry Ritchie's Excellent Flappy Bird Clone
Ported from QB64
Code: QB64: [Select]
  1. Import FlappyBird From ""
Try it on QBJS-Dev

I'd be very interested to hear your thoughts.

Offline CharlieJV

  • Newbie
  • Posts: 89
Re: QBJS - Import code from external .bas files
« Reply #1 on: April 15, 2022, 05:34:06 pm »
First thought: that is very cool.

Sober second thought: what are the security implications ?  For example, what happens if an imported file from some source is maliciously modified?

It would be good to well-document whatever risks, and how to mitigate risks.  (I'm no web guy, so my security hat is very much of the base-level-amateur kind.)

I would only import into my programs source code in my control (i.e. my web domain), but I think I'd be okay with the mechanism for running, sandboxed, a program that is hosted wherever.

Offline dbox

  • Newbie
  • Posts: 80
Re: QBJS - Import code from external .bas files
« Reply #2 on: April 15, 2022, 07:31:34 pm »
@CharlieJV, thanks for the feedback.  Security is always a good thing to keep in mind.  This feature is modeled to some degree after the standard way JavaScript allows supporting libraries to be included (  Like JS these libraries can be loaded from a relative path or from an external location. Even though the modern browser is sandboxed to a large degree to prevent malicious activity discretion is always required of the developer.

In addition, I’m also working on an Export feature for this release which will allow you to export the compiled JavaScript and supporting html files as a zip.  The compiled version will incorporate content from any included libraries. This can then be run standalone or is ready to be uploaded to a sharing site like This is also available on the qbjs-dev site, where the most current dev build is deployed.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: QBJS - Import code from external .bas files
« Reply #3 on: April 15, 2022, 10:14:18 pm »
This project continues to impress! Good Stuff!