Skip to content

Commit

Permalink
Merge pull request #1 from viodotcom/eval-json-segfault
Browse files Browse the repository at this point in the history
JSON segfault crisis: ditch cgo
  • Loading branch information
tokahuke authored Jul 13, 2024
2 parents 2d6e735 + dca782f commit db67eec
Show file tree
Hide file tree
Showing 26 changed files with 860 additions and 688 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/build-so.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Build CJyafn Shared Object

on:
push:
branches:
- stable
workflow_dispatch:


jobs:
build-so:
steps:
- name: Check out repository code
uses: actions/checkout@v4
with:
ref: stable
- name: Build shared object
run: |
bash ./utils/build-linux-so.sh
- name: Artifact update
uses: actions/upload-artifact@v4
with:
name: linux-x64
if-no-files-found: error
path: target/release/libcjyafn.so
retention-days: 1

release:
needs: [build-so]
runs-on: ubuntu-latest
steps:
- name: Download so
uses: actions/download-artifact@v4
with:
merge-multiple: true
- name: Release
uses: softprops/action-gh-release@v2
with:
files: "*.so"
tag_name: commit-id=${{ github.sha }}
prerelease: false
make_latest: true
54 changes: 0 additions & 54 deletions .github/workflows/build-wheel.yaml

This file was deleted.

15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
.PHONY: cjyafn jyafn-python

qbe:
cd vendored/qbe && make qbe
cd vendored/qbe && make clean && make qbe && ./qbe -h

cjyafn: qbe
cargo build --release

jyafn-python: qbe
cd jyafn-python && maturin build --release

goexport: cjyafn
cp target/release/cjyafn.h jyafn-go/pkg/jyafn
cp target/release/libcjyafn.a jyafn-go/pkg/jyafn

install: jyafn-python
python$(py) -m pip install --force-reinstall target/wheels/*.whl

Expand All @@ -27,3 +23,12 @@ build-macos-wheels:
bash ./utils/build-macos-wheels.sh

build-wheels: build-linux-wheels build-macos-wheels

build-linux-so:
bash ./utils/build-linux-so.sh

install-dylib: cjyafn
sudo cp target/release/libcjyafn.dylib /usr/local/lib

install-so: cjyafn
sudo cp target/release/libcjyafn.so /usr/local/lib
2 changes: 1 addition & 1 deletion cjyafn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
[lib]
doc = false
name = "cjyafn"
crate-type = ["staticlib"]
crate-type = ["cdylib"]

[dependencies]
get-size = "0.1.4"
Expand Down
64 changes: 64 additions & 0 deletions cjyafn/jyafn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* One simple example of the use of cjyafn in C directly, to debug the interface between
* C and Go. To run, do:
* ```
* gcc jyafn.c ../target/release/libcjyafn.a -lm && ./a.out
* ```
*/

#include <stdio.h>
#include "cjyafn.h"

typedef struct {
char* buffer;
long length;
} READ;

READ read_file() {
char* buffer = 0;
long length;
READ contents;
FILE * f = fopen("testdata/silly-map.jyafn", "rb");

if (f) {
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = malloc (length);
if (buffer) {
fread (buffer, 1, length, f);
}
fclose (f);
}

contents.buffer = buffer;
contents.length = length;

return contents;
}

void main() {
READ contents = read_file();
if (contents.buffer == 0) {
printf("failed to read file\n");
exit(1);
}

Outcome out = function_load(contents.buffer, contents.length);
if (out.err != 0) {
printf(error_to_string(out.err));
// free(out.err);
exit(1);
}

void* func = out.ok;

out = function_eval_json(func, "{\"a\": 4.0, \"x\": \"a\"}");
if (out.err != 0) {
printf(error_to_string(out.err));
// free(out.err);
exit(1);
}

printf("outcome = %s\n", out.ok);
}
Loading

0 comments on commit db67eec

Please sign in to comment.