I am writing a video game in gambit scheme. I want to provide the ability for users to write plugins for the game in a minimal "safe" environment, and distribute these plugins. How can I do this?
Is this a good use-case for a DSL?
Please design the DSL to be similar in syntax and semantics to gambit. Is this implementation simpler? Also we want to expose large parts of the r7rs spec (and gambit scheme extensions) to allow for complex plugin development while still staying safe.
Write a brief summary to be given to a developer unfamiliar with scheme or the specifics of the game
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Absolutely, this is a classic use-case for a DSL (Domain-Specific Language)—especially when you want to provide a controlled, safe, and extensible scripting environment for plugins. Scheme (and Gambit in particular) is well-suited for this, as it is highly extensible and can be sandboxed with care.
Below, I’ll address your questions and provide a design outline.
1. Why a DSL?
A DSL allows you to:
Restrict what plugin authors can do (for safety/security).
Expose only the APIs you want (game-specific functions, safe standard library).
Control resource usage (memory, CPU, etc.).
Provide a familiar syntax (Scheme-like) for easier onboarding.
2. Sandboxing Scheme for Plugins
Scheme is a great base for a plugin DSL because:
It’s minimal and expressive.
Gambit allows you to control the environment (namespace, available functions).
You can provide a "safe" subset of the language.
Sandboxing typically involves:
Running plugin code in a restricted environment (namespace/module).
Exposing only whitelisted functions and data.
Preventing access to the filesystem, network, or dangerous primitives.
Safe R7RS core: define, lambda, if, let, cond, map, etc.
Immutable data structures (no mutation unless safe).
No direct I/O (unless via safe APIs).
No eval, load, or similar.
Game API: Only functions you expose.
4. Implementation Outline
In Gambit Scheme:
Create a restricted environment (namespace/module) for plugins.
Whitelist safe R7RS functions and your game API.
Load plugin code using eval or similar, but in the restricted environment.
Disallow or stub out dangerous functions (file/network I/O, system calls).
Optionally: Use resource limits (timeouts, memory quotas).
Example: Creating a Safe Environment
scheme
(define safe-env
(let ((env (make-environment)))
;; Import safe R7RS functions
(import (only (scheme base) define lambda if let cond map ...))
;; Import game API
(import (only (game api) send-message update-scoreboard ...))
env))
;; Load and evaluate plugin code in safe-env
(eval plugin-source safe-env)
5. Is this Simpler?
Using a DSL (subset of Scheme) is simpler than writing a new interpreter.
Sandboxing is the main complexity, but Gambit’s module system helps.
Exposing R7RS is easy—just import the safe subset.
Extending with game APIs is straightforward.
6. Summary for Developers (Non-Scheme/Game-Specific)
Plugin System Overview
Our game supports user-created plugins written in a safe, Scheme-like scripting language. Plugins can respond to game events, interact with players, and modify game state using a set of provided APIs. The scripting environment is sandboxed for security: plugins can only access safe parts of the language and the game API, with no direct access to files, the network, or the operating system.
Example Plugin:
scheme
(define-plugin "hello"
(on-event 'player-join
(lambda (player)
(send-message player "Welcome!")))
)
If you know basic programming, you can write plugins by defining event handlers and using the provided functions. No prior Scheme experience is required—our documentation covers the basics!
7. Next Steps
Define your game API (what plugins can do).
Implement the sandbox (restricted environment).
Document the available functions and language subset.
Provide examples and a getting-started guide.
Let me know if you want a more detailed code example, or help with a specific part of the implementation!
User
Between gambit/r7rs there are hundreds of functions. Do I need to explicitly whitelist/blacklist each function?
Assistant
Great question! You do not need to manually list every single function—but you do need to be deliberate about what is available in the plugin environment. Here’s how you can approach this in practice, especially with Gambit and R7RS:
1. Default: Nothing is Available
When you create a new environment (namespace) in Scheme, by default, it is usually empty. You then import only what you want. This is the safest approach.
Whitelist: Only import the modules and functions you want.
Blacklist: Import everything, then remove/block dangerous functions (riskier, more error-prone).
Whitelisting is strongly recommended.
2. How to Whitelist in Scheme (R7RS/Gambit)
You can use the import form to bring in only the safe parts of the language. For example:
scheme
(import (only (scheme base) define lambda if let cond map ...))
This imports only the listed identifiers from the (scheme base) module.
You can do the same for other modules, e.g., (scheme list), (scheme vector), etc.
You do not need to list every function in R7RS—just the ones you want to expose.
3. How to Expose Your Game API
You can define your own module, e.g., (game api), and import only the safe functions you want plugin authors to use.
Gambit provides many extensions. If you want to expose some, you can do so by importing them explicitly, or by re-exporting them from your own module.
5. What If You Want to Blacklist?
If you import a whole module (e.g., (scheme base)), you would need to explicitly remove or override dangerous functions. This is error-prone, as new functions could be added in future versions.