Code Monkey home page Code Monkey logo

zapata's Introduction

Zapata is a wrapper around the Wren scripting language written in Zig

See it in action:

const std = @import("std");

const zapata = @import("./src/main.zig");
const Configuration = zapata.Configuration;
const Vm = zapata.Vm;
const ForeignClass = zapata.ForeignClass;
const Method = zapata.Method;

const EmptyUserData = struct {};

const ToWrenAndBack = struct {
    const Self = @This();

    n: i32,

    pub fn initialize1(self: *Self, vm: *Vm, n: i32) void {
        self.n = n;
    }

    pub fn call(self: *Self, vm: *Vm, message: []const u8) []const u8 {
        if (self.n == 42 and std.mem.eql(u8, "Though Wren to Zig...", message)) {
            std.debug.print("{s}\n", .{message});
            return "...and back!";
        }
        return "";
    }
};

pub fn main() !void {
    var config = Configuration{};
    config.registerForeignClasses(.{
        ForeignClass("ToWrenAndBack", ToWrenAndBack),
    });

    var vm: Vm = undefined;
    try config.newVmInPlace(EmptyUserData, &vm, null);
    defer vm.deinit();

    try vm.interpret("main",
        \\foreign class ToWrenAndBack {
        \\  construct new(n) {}
        \\  foreign call(message)
        \\}
        \\
        \\var twab = ToWrenAndBack.new(42)
    );

    const receiver = vm.makeReceiver("main", "twab");
    defer receiver.deinit();
    const call_sig = vm.makeCallHandle("call(_)");
    defer call_sig.deinit();

    const call = Method([]const u8, .{[]const u8}).init(receiver, call_sig);

    std.debug.print("{s}\n", .{try call.call(.{"Though Wren to Zig..."})});
}

Status

The library is usable, but some Wren features are missing. Current (known) unsupported features are lists, maps, and per-module foreign classes.

Building

Currently, Zapata depends on a as-of-yet unaccepted patch to Wren to enable per-VM allocators. Because of this, simply put wren.h into vendor/wren/include and libwren.a into vendor/wren/lib. If vendor/wren exists, it will be used automatically, otherwise a system-wide installation of Wren is used.

If you've got a compatible Wren available, building the library is just a

zig build

away.

Documentation

Is lacking.

Contributing

Fixes, improvements, and added tests are very welcome. Major style changes would need to be discussed โ€“ Wren is simple enough to embed that a wrapper is not strictly necessary, so this library will keep a slightly opinionated style designed for specific use cases.

Name

Zapata is named after a species of wren that begins with a 'z'. That's it.

zapata's People

Contributors

koljakube avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.