Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,24 @@
} // extraArgs;
});

buildCApi = { pnameSuffix ? "", extraBuildArgs ? "", extraArgs ? { } }:
craneLib.buildPackage ({
inherit
pname
pnameSuffix
src
version
cargoArtifacts;

cargoExtraArgs = "${cargoBuildExtraArgs} ${extraBuildArgs} --features capi --package nickel-lang";
CARGO_PROFILE = profile;

postInstall = ''
mkdir -p $out/include
${pkgs.rust-cbindgen}/bin/cbindgen nickel --lockfile ./Cargo.lock --output $out/include/nickel_lang.h --cpp-compat
'';
} // extraArgs);

# In addition to external dependencies, we build the lalrpop file in a
# separate derivation because it's expensive to build but needs to be
# rebuilt infrequently.
Expand Down Expand Up @@ -429,6 +447,24 @@
extraArgs = { meta.mainProgram = "nickel"; };
});

nickel-lang-c = buildCApi { pnameSuffix = "-c-api"; };

nickel-lang-c-test = pkgs.stdenv.mkDerivation {
name = "nickel-lang-c-test";
inherit version;
src = ./nickel/capi-tests;

buildPhase = ''
mkdir -p $out/bin
gcc -I "${nickel-lang-c}/include/" run_tests.c "${nickel-lang-c}/lib/libnickel_lang.a" -lm -o $out/bin/run_tests
'';

doCheck = true;
checkPhase = ''
$out/bin/run_tests
'';
};

benchmarks = craneLib.mkCargoDerivation {
inherit pname src version cargoArtifacts env;

Expand Down Expand Up @@ -485,6 +521,7 @@
pkgs.cargo-flamegraph
pkgs.cargo-insta
pkgs.cargo-nextest
pkgs.rust-cbindgen
pkgs.nixpkgs-fmt
pkgs.nodejs
pkgs.yarn
Expand Down Expand Up @@ -661,6 +698,7 @@
nickel-lang
nickel-lang-pkg
nickel-lang-nix
nickel-lang-c
benchmarks
cargoArtifacts;
default = packages.nickel-lang;
Expand Down Expand Up @@ -705,6 +743,7 @@
clippy
checkRustDoc
nickel-lang
nickel-lang-c-test
rustfmt;
nickelWasm = buildNickelWasm { profile = "dev"; };
inherit vscodeExtension stdlibTests;
Expand Down
6 changes: 6 additions & 0 deletions nickel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ repository.workspace = true
homepage.workspace = true
readme.workspace = true

[lib]
crate-type = ["lib", "staticlib", "cdylib"]

[features]
capi = []

[dependencies]
codespan-reporting.workspace = true
indexmap.workspace = true
Expand Down
94 changes: 94 additions & 0 deletions nickel/capi-tests/run_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "nickel_lang.h"

const char* EXAMPLE = " \
{ \
foo = 1, \
} \
";

const char* BAD_EXAMPLE = " \
{ \
foo | String = 1, \
} \
";

const char* TRACE_EXAMPLE = "std.trace \"hi\" 1";

struct buffer {
char *ptr;
uintptr_t capacity;
uintptr_t len;
};

struct buffer new_buffer() {
struct buffer buf = {
malloc(128),
128,
0,
};
return buf;
}

uintptr_t write_callback(void *context, uint8_t const* buf, uintptr_t len) {
struct buffer *out_buf = (struct buffer*) context;
while (out_buf->capacity <= out_buf->len + len) {
out_buf->capacity *= 2;
out_buf->ptr = realloc(out_buf->ptr, out_buf->capacity);
assert(out_buf->ptr != NULL);
}

memcpy(out_buf->ptr + out_buf->len, buf, len);
out_buf->len += len;
out_buf->ptr[out_buf->len] = '\0';

return len;
}

int main() {
nickel_context *ctx = nickel_context_alloc();
nickel_expr *expr = nickel_expr_alloc();
nickel_error *error = nickel_error_alloc();

nickel_result result = nickel_context_eval_deep(ctx, EXAMPLE, expr, error);
assert(result == NICKEL_RESULT_OK);

assert(nickel_expr_is_record(expr));
nickel_record const *rec = nickel_expr_as_record(expr);
assert(nickel_record_len(rec) == 1);

char const* key;
uintptr_t len;
nickel_expr *val = nickel_expr_alloc();
nickel_record_key_value_by_index(rec, 0, &key, &len, val);

assert(len == 3);
assert(!strncmp(key, "foo", 3));
assert(nickel_expr_is_number(val));

// Test error reporting
result = nickel_context_eval_deep(ctx, BAD_EXAMPLE, expr, error);
assert(result == NICKEL_RESULT_ERR);

struct buffer buf = new_buffer();
nickel_error_display(error, write_callback, &buf, NICKEL_ERROR_FORMAT_ANSI_TEXT);
fputs(buf.ptr, stderr);
assert(strstr(buf.ptr, "contract broken by the value") != NULL);

// Test tracing
buf.len = 0;
nickel_context_set_trace_callback(ctx, write_callback, NULL, &buf);
result = nickel_context_eval_deep(ctx, TRACE_EXAMPLE, expr, error);
assert(result == NICKEL_RESULT_OK);
assert(!strcmp(buf.ptr, "std.trace: hi\n"));

nickel_expr_free(val);
nickel_expr_free(expr);
nickel_error_free(error);
nickel_context_free(ctx);
}
14 changes: 14 additions & 0 deletions nickel/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
header = "// SPDX-License-Identifier: MIT"
sys_includes = ["stdint.h"]
no_includes = true
include_guard = "NICKEL_LANG_H"
tab_width = 4
style = "Type"
language = "C"
cpp_compat = true

[export]

[const]
allow_static_const = false
allow_constexpr = false
Loading
Loading