Gambas Playground

The Gambas playground is a small web tool to write, run and share Gambas code Snippets. It is currently hosted here: - http://gambas.one/playground.

Overview

The Playground allows you to write a small Gambas scripts using an in-browser editor, then run it on the Playground's server and see the output of the script, without needing to install anything.

It also allows you to share links that point directly to your code on the Playground, using GitHub's Gist service.

Example 1

For i As Integer = 1 To 50
  Print "Count to "; i
Next

A simple script that counts from 1 to 50.

Example 2

'How many times do you need to loop through randomly generated numbers before you randomly get '123456789'?

Public Sub Main()
Dim sSorted As String = "123456789"                       'The desired outcome
Dim sTest, sChr As String                                 'Various strings
Dim iCounter As Integer                                   'Loop counter

Do
  Inc iCounter                                            'Increase counter value
  Repeat                                                  'Repeat 
    sChr = Chr(Rand(49, 57))                              'Get a random number and convert it to a character e.g. 49="1"
    If Not InStr(sTest, sChr) Then sTest &= sChr          'If the random character is not in sTest then add it
  Until Len(sTest) = 9                                    'Loop until sTest has 9 characters
  If sTest = sSorted Then Break                           'If sTest = sSorted then get out of the loop
  sTest = ""                                              'Empty sTest and try again
Loop

Print "Solved in " & Str(iCounter) & " loops"             'Print the result

End

A script that can end up with 750000 loops. You may need to run this script a few times to get it to work.

Using components

Gambas components can be used in the using the USE keyword, like any other Gambas script.

However, since code snippets run in a completely isolated environment, components that rely on User Interfaces (GUI or CLI) or networking (HTTP, FTP, Databases …) are not available.

Example

Use "gb.xml"

Dim doc As New XmlDocument
Print doc.ToString(True)

This script creates an empty XML Document using the gb.xml component, and then prints its contents (with formatting enabled). Playground link

Available Components

All the components installed by Gambas by default are available to use including: -

gb.args, gb.clipper, gb.complex, gb.compress, gb.data, gb.eval, gb.eval.highlight, gb.image.effect, gb.image, gb.inotify, gb.markdown, gb.option, gb.settings, gb.signal, gb.util, gb.util.web, gb.vb, gb.xml, gb.xml.html, gb.gsl

Limitations

Since this web service is literally about arbitrary code execution, a few limitations are in place in order to prevent abuse.

Although these should not bother you in most cases, you might want to keep them in mind should you try to run some computationally expensive code.

  • Time limit : Scripts cannot run for more than 5 seconds. Scripts exceeding this limit will get killed (i.e. the Docker container will be destroyed, denying any kind of recovery).

  • Memory limit : Script cannot allocate more than 16 MiB of RAM. Doing so will cause the kernel to raise a SIGSEGV signal (segmentation fault), hence killing the script. Note that this limit is set for the whole environment, therefore the Gambas runtime's memory overhead is also counted towards this limit.

  • Network limitations : No network access is possible : no network card is accessible, and sockets cannot be opened.

  • Filesystem limitations : While any file in the filesystem can be read from, written to, created or deleted, all changes will be discarded when the script ends. Files should therefore be used only as temporary storage.

Code storage

The last edited snippet is locally saved into the browser's LocalStorage and restored when opening the (if not from a shared link). This allows to prevent some unfortunate accidents, such as power outages, browser crashes, and chair/keyboard interface defects.

Delete shared code

If shared code happens to contain sensitive data (such as passwords, private keys …), you can contact the GitHub support and provide them with the Gist's link (generated along the link when sharing), or directly the Gist's ID (included in link URIs), allowing them to manually delete it.

If you have a GitHub account you can manually create a gist, paste your script's contents in it, and then manually craft a link to the using the following pattern :

http://gambas.one/playground/?gist=your_gist_id_here

Thanks to

Adrien Prokopowicz who created the original playground and Matthew Collins who modified it to suit our needs.