-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[language binding] add c-api and java-api
- Loading branch information
Showing
16 changed files
with
275 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
cc_library( | ||
name = "pscm_c_api", | ||
srcs = ["pscm_c_api.cpp"], | ||
hdrs = ["pscm_c_api.h"], | ||
copts = ["-std=c++20"], | ||
implementation_deps = ["//:pscm"], | ||
includes = ["."], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "pscm_c_api.h" | ||
#include <pscm/Scheme.h> | ||
using namespace pscm; | ||
|
||
void *pscm_create_scheme() { | ||
return new Scheme(); | ||
} | ||
|
||
void pscm_destroy_scheme(void *scm) { | ||
auto p = (Scheme *)scm; | ||
delete p; | ||
} | ||
|
||
void *pscm_eval(void *scm, const char *code) { | ||
auto p = (Scheme *)scm; | ||
auto ret = p->eval(code); | ||
return new Cell(ret); | ||
} | ||
|
||
const char *pscm_to_string(void *value) { | ||
auto p = (Cell *)value; | ||
auto s = p->to_std_string(); | ||
char *c_str = new char[s.size() + 1]; | ||
strcpy(c_str, s.c_str()); | ||
c_str[s.size()] = '\0'; | ||
return c_str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
extern "C" { | ||
void *pscm_create_scheme(); | ||
void pscm_destroy_scheme(void *scm); | ||
void *pscm_eval(void *scm, const char *code); | ||
const char *pscm_to_string(void *value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
java_library( | ||
name = "pscm_java_api", | ||
srcs = ["dev/pscm/PSCMScheme.java"], | ||
visibility = ["//visibility:public"], | ||
deps = select({ | ||
"@platforms//os:android": [], | ||
"//conditions:default": [":pscm-jni"], | ||
}), | ||
) | ||
|
||
cc_library( | ||
name = "pscm_java_binding", | ||
srcs = ["pscm_java_binding.cpp"], | ||
copts = ["-std=c++20"], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
":copy_jni_hdr_lib", | ||
"//binding/c:pscm_c_api", | ||
], | ||
alwayslink = True, | ||
) | ||
|
||
cc_binary( | ||
name = "pscm-jni", | ||
linkshared = True, | ||
deps = [":pscm_java_binding"], | ||
) | ||
|
||
java_test( | ||
name = "pscm_java_api_test", | ||
srcs = ["test/PSCMSchemeTest.java"], | ||
test_class = "test.PSCMSchemeTest", | ||
deps = [ | ||
":pscm_java_api", | ||
"@maven//:junit_junit", | ||
], | ||
) | ||
|
||
java_binary( | ||
name = "pscm_java_api_example", | ||
srcs = ["example/PSCMSchemeExample.java"], | ||
main_class = "PSCMSchemeExample", | ||
deps = [ | ||
":pscm_java_api", | ||
], | ||
) | ||
|
||
genrule( | ||
name = "copy_link_jni_md_header", | ||
srcs = select({ | ||
"@platforms//os:macos": ["@bazel_tools//tools/jdk:jni_md_header-darwin"], | ||
"@platforms//os:linux": ["@bazel_tools//tools/jdk:jni_md_header-linux"], | ||
"//conditions:default": [], | ||
}), | ||
outs = ["jni_md.h"], | ||
cmd = "cp -f $< $@", | ||
) | ||
|
||
genrule( | ||
name = "copy_link_jni_header", | ||
srcs = ["@bazel_tools//tools/jdk:jni_header"], | ||
outs = ["jni.h"], | ||
cmd = "cp -f $< $@", | ||
) | ||
|
||
cc_library( | ||
name = "copy_jni_hdr_lib", | ||
hdrs = [ | ||
":copy_link_jni_header", | ||
":copy_link_jni_md_header", | ||
], | ||
includes = ["."], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package dev.pscm; | ||
|
||
public class PSCMScheme { | ||
|
||
private long scm = 0; | ||
|
||
public PSCMScheme() { | ||
init(); | ||
} | ||
|
||
public void init() { | ||
if (scm == 0) { | ||
scm = createScheme(); | ||
} | ||
} | ||
|
||
public String eval(String code) { | ||
return evalSchemeCode(scm, code); | ||
} | ||
|
||
/** | ||
* A native method that is implemented by the 'native-lib' native library, | ||
* which is packaged with this application. | ||
*/ | ||
public native long createScheme(); | ||
public native String evalSchemeCode(long scm, String code); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import dev.pscm.PSCMScheme; | ||
public class PSCMSchemeExample { | ||
|
||
static { | ||
// load shared library | ||
System.loadLibrary("pscm-jni"); | ||
} | ||
|
||
public static void main(String args[]) { | ||
System.out.println("Hello World"); | ||
PSCMScheme scm = new PSCMScheme(); | ||
// eval (version) | ||
String ret = scm.eval("(version)"); | ||
System.out.println(ret); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "pscm_c_api.h" | ||
#include <jni.h> | ||
#include <string> | ||
|
||
extern "C" JNIEXPORT jlong JNICALL Java_dev_pscm_PSCMScheme_createScheme(JNIEnv *env, jobject /* this */) { | ||
auto scm = pscm_create_scheme(); | ||
return (jlong)scm; | ||
} | ||
|
||
extern "C" JNIEXPORT jstring JNICALL Java_dev_pscm_PSCMScheme_evalSchemeCode(JNIEnv *env, jobject /* this | ||
*/ | ||
, | ||
jlong scm, jstring code) { | ||
auto p = (void *)scm; | ||
auto c_str = env->GetStringUTFChars(code, nullptr); | ||
auto ret = pscm_eval(p, c_str); | ||
auto s = pscm_to_string(ret); | ||
return env->NewStringUTF(s); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package test; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import dev.pscm.PSCMScheme; | ||
import org.junit.Test; | ||
|
||
public class PSCMSchemeTest { | ||
|
||
static { | ||
System.loadLibrary("pscm-jni"); | ||
} | ||
|
||
// public static void main(String args[]) { | ||
// System.out.println("Hello World"); | ||
// PSCMScheme scm = new PSCMScheme(); | ||
// String ret = scm.eval("(version)"); | ||
// System.out.println(ret); | ||
// } | ||
|
||
@Test | ||
public void testAdd() { | ||
PSCMScheme scm = new PSCMScheme(); | ||
String ret = scm.eval("(+ 2 3)"); | ||
assertEquals("should return 5 when calculate (+ 2 3)", "5", ret); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.