-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
342 changed files
with
138,978 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/.idea | ||
/target | ||
Cargo.lock | ||
/cmake-build-debug | ||
dist-newstyle/ |
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 @@ | ||
{"buildTargets":["all","build","clean","example","fmt"],"launchTargets":[],"customConfigurationProvider":{"workspaceBrowse":{"browsePath":[],"compilerArgs":[]},"fileIndex":[]}} |
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,5 @@ | ||
make --dry-run --always-make --keep-going --print-directory | ||
make: Entering directory '/Users/hoangpq/CLionProjects/r-binding' | ||
cargo build | ||
make: Leaving directory '/Users/hoangpq/CLionProjects/r-binding' | ||
|
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,5 @@ | ||
{ | ||
"makefile.extensionOutputFolder": "./.vscode", | ||
"deno.enable": true, | ||
"nixEnvSelector.nixFile": "${workspaceRoot}/shell.nix" | ||
} |
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Revision history for r-binding | ||
|
||
## 0.1.0.0 -- YYYY-mm-dd | ||
|
||
* First version. Released on an unsuspecting world. |
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,12 @@ | ||
cmake_minimum_required(VERSION 3.21) | ||
project(r_binding) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
include_directories(r_sys/r) | ||
include_directories(r_sys/src/include) | ||
include_directories(r_sys/src/include/R_ext) | ||
|
||
add_executable(r_binding | ||
r_sys/src/binding.cc | ||
r_sys/src/binding.h) |
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,11 @@ | ||
[package] | ||
name = "r-binding" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
path = "src/bindings.rs" | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
r_sys = { path = "./r_sys" } |
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,11 @@ | ||
build: | ||
cargo build | ||
|
||
fmt: | ||
cargo fmt | ||
deno fmt --ignore=target/ | ||
|
||
example: | ||
deno run --unstable -A examples/simple.ts | ||
|
||
all: fmt build example |
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 @@ | ||
# deno_r |
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,145 @@ | ||
{-# OPTIONS_GHC -fplugin-opt=Foreign.Storable.Generic.Plugin:-v1 #-} | ||
{-# LANGUAGE ForeignFunctionInterface #-} | ||
|
||
module Main where | ||
|
||
import Control.Applicative | ||
import Foreign | ||
import Foreign.C | ||
import Prelude hiding (lex) | ||
import Text.ParserCombinators.ReadP | ||
import Text.Read.Lex | ||
import GHC.Ptr | ||
|
||
import Data.List (elemIndex) | ||
import Control.Monad | ||
import Control.Concurrent | ||
import Control.Concurrent.STM | ||
import Foreign.Marshal.Alloc | ||
import Foreign.Marshal.Array (mallocArray) | ||
import Data.Maybe (fromMaybe) | ||
import Foreign.Storable (Storable) | ||
|
||
import Control.Concurrent | ||
import Control.Concurrent.STM | ||
|
||
foreign export ccall "eval" c_eval :: CString -> Ptr CInt -> FunPtr (CInt -> IO ()) -> IO (Ptr CInt) | ||
foreign export ccall "goroutine" goroutine :: FunPtr (CInt -> IO ()) -> IO CInt | ||
foreign export ccall "timer" timer :: FunPtr (CInt -> IO ()) -> Int -> IO () | ||
foreign import ccall "dynamic" mkFun :: FunPtr (CInt -> IO ()) -> (CInt -> IO ()) | ||
foreign import ccall "sin" c_sin :: Double -> IO Double | ||
foreign export ccall "array" array :: IO (Ptr CInt) | ||
|
||
mkArray :: [Int] -> IO (Ptr CInt) | ||
mkArray vals = do | ||
ptr <- mallocArray $ ((+1) . length) vals | ||
_ <- pokeElemOff ptr 0 $ (intToCInt . length) vals | ||
forM_ vals $ \v -> do | ||
let idx = fromMaybe 0 $ v `elemIndex` vals | ||
pokeElemOff ptr (idx + 1) (intToCInt v) | ||
return ptr | ||
|
||
array :: IO (Ptr CInt) | ||
array = mkArray [10,20..100] | ||
|
||
f :: String -> IO () | ||
f from = forM_ [0..2] | ||
(\i -> putStrLn $ from ++ ":" ++ show i) | ||
|
||
fac :: Integer -> Integer | ||
fac 0 = 1 | ||
fac n = n * fac (n - 1) | ||
|
||
addNumbers :: TVar Int -> TVar Int -> IO Int | ||
addNumbers var1 var2 = do | ||
lock <- newMVar () | ||
|
||
forkIO $ atomically $ do | ||
val1 <- readTVar var1 | ||
writeTVar var1 (val1 + 1) | ||
|
||
forkIO $ atomically $ do | ||
val2 <- readTVar var2 | ||
writeTVar var2 (val2 + 2) | ||
|
||
atomically $ do | ||
val1 <- readTVar var1 | ||
val2 <- readTVar var2 | ||
return (val1 + val2) | ||
|
||
goroutine f_ptr = do | ||
-- forkIO $ f 1 | ||
msg1 <- atomically newTQueue | ||
msg2 <- atomically newTQueue | ||
forkIO $ do | ||
atomically $ writeTQueue msg1 "ping" | ||
forkIO $ do | ||
atomically $ writeTQueue msg2 "pong" | ||
_ <- atomically $ readTQueue msg1 | ||
_ <- atomically $ readTQueue msg2 | ||
f 3 | ||
putStrLn "Done!" | ||
|
||
var1 <- atomically $ newTVar 0 | ||
var2 <- atomically $ newTVar 100 | ||
|
||
intToCInt <$> addNumbers var1 var2 | ||
|
||
-- return 1 | ||
where | ||
f = mkFun f_ptr | ||
|
||
intToCInt :: Int -> CInt | ||
intToCInt = fromIntegral | ||
{-# INLINE intToCInt #-} | ||
|
||
timer ptr t = do | ||
msg <- atomically newTQueue | ||
forkIO $ do | ||
threadDelay (t * 1000000) | ||
f (intToCInt t) | ||
atomically $ writeTQueue msg "ping" | ||
_ <- atomically $ readTQueue msg | ||
putStrLn "Done!" | ||
where | ||
f = mkFun ptr | ||
|
||
c_eval s r f_ptr = do | ||
cs <- peekCAString s | ||
-- f 42 | ||
case hs_eval cs of | ||
Nothing -> return nullPtr | ||
Just x -> do | ||
poke r x | ||
return r | ||
where | ||
f = mkFun f_ptr | ||
|
||
hs_eval :: String -> Maybe CInt | ||
hs_eval inp = case readP_to_S expr inp of | ||
(a,_) : _ -> Just a | ||
[] -> Nothing | ||
|
||
expr = addition <* expect EOF | ||
|
||
addition = chainl1 multiplication add | ||
where | ||
add = expect (Symbol "+") >> return (+) | ||
|
||
multiplication = chainl1 atom mul | ||
where | ||
mul = expect (Symbol "*") >> return (*) | ||
|
||
atom = number <|> between lp rp addition | ||
|
||
number = do | ||
Number n <- lex | ||
case numberToInteger n of | ||
Just i -> return (fromIntegral i) | ||
Nothing -> pfail | ||
|
||
lp = expect (Punc "(") | ||
rp = expect (Punc ")") | ||
|
||
main :: IO () | ||
main = undefined |
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,12 @@ | ||
#include <HsFFI.h> | ||
#if defined(__cplusplus) | ||
extern "C" { | ||
#endif | ||
extern HsPtr eval(HsPtr a1, HsPtr a2, HsFunPtr a3); | ||
extern HsInt32 goroutine(HsFunPtr a1); | ||
extern void timer(HsFunPtr a1, HsInt a2); | ||
extern HsPtr array(void); | ||
#if defined(__cplusplus) | ||
} | ||
#endif | ||
|
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,12 @@ | ||
#include <HsFFI.h> | ||
#if defined(__cplusplus) | ||
extern "C" { | ||
#endif | ||
extern HsPtr eval(HsPtr a1, HsPtr a2, HsFunPtr a3); | ||
extern HsInt32 goroutine(HsFunPtr a1); | ||
extern void timer(HsFunPtr a1, HsInt a2); | ||
extern HsPtr array(void); | ||
#if defined(__cplusplus) | ||
} | ||
#endif | ||
|
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,24 @@ | ||
#include <stdio.h> | ||
|
||
extern int *eval(const char *, int *); | ||
|
||
#define INPUT_LEN 500 | ||
|
||
int main(int argc, char*argv[]) | ||
{ | ||
int r; | ||
char input[INPUT_LEN]; | ||
|
||
for (;;) { | ||
printf("> "); | ||
if (fgets(input, INPUT_LEN, stdin) == NULL) break; | ||
if (eval(input, &r) != NULL) { | ||
printf("%d\n", r); | ||
} else { | ||
printf("syntax error\n"); | ||
} | ||
} | ||
printf("\n"); | ||
|
||
return 0; | ||
} |
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,6 @@ | ||
#include <stdio.h> | ||
|
||
int main(int argc, char **argv) { | ||
fprintf(stderr, "Hello, World!\n"); | ||
return 0; | ||
} |
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,35 @@ | ||
/* This file is hsbracket.c. */ | ||
#include <stdio.h> | ||
#include <HsFFI.h> | ||
|
||
#if defined(__cplusplus) | ||
extern "C" { | ||
#endif | ||
|
||
void __attribute__((constructor)) my_enter(); | ||
|
||
void __attribute__((destructor)) my_exit(); | ||
|
||
typedef void (*callback)(int); | ||
|
||
extern void register_cb(callback *f) { | ||
printf("register_cb %p\n", f); | ||
} | ||
|
||
extern int *eval(const char *, int *); | ||
|
||
extern void *goroutine(callback *); | ||
|
||
extern void my_enter(void) { | ||
static char *argv[] = {"libEval.dylib", 0}, **argv_ = argv; | ||
static int argc = 1; | ||
hs_init(&argc, &argv_); | ||
} | ||
|
||
extern void my_exit(void) { | ||
hs_exit(); | ||
} | ||
|
||
#if defined(__cplusplus) | ||
} | ||
#endif |
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,7 @@ | ||
#include <string.h> | ||
#include "jni.h" | ||
|
||
JNIEXPORT jstring JNICALL | ||
Java_com_akavel_hello2_HelloActivity_stringFromJNI(JNIEnv *env, jobject thiz) { | ||
return (*env)->NewStringUTF(env, "Hello from JNI..!"); | ||
} |
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,33 @@ | ||
# get C header | ||
# ghc -c -O Eval.hs | ||
|
||
# nix-shell -p "ghc.withPackages (pkgs: with pkgs; [ hakyll pandoc ])" | ||
GHC_VERSION=$(ghc --numeric-version) | ||
LIBS_DIR=libs | ||
|
||
echo "Using ghc: $GHC_VERSION" | ||
|
||
# Manually | ||
#ghc -O2 -dynamic -shared -fPIC \ | ||
# -o libEval.dylib Eval.hs hsbracket.c \ | ||
# -L"$(ghc --print-libdir)/rts" \ | ||
# -l"HSrts-ghc$GHC_VERSION" | ||
|
||
# With cabal | ||
cabal build | ||
ghc -O2 -fPIC -c app/c-src/hsbracket.c | ||
ghc -O2 -dynamic -shared -fPIC -o $LIBS_DIR/libEval \ | ||
app/Main.hs app/c-src/hsbracket.o -l"HSrts-ghc$GHC_VERSION" | ||
|
||
# gcc -O2 -c calculator.c | ||
# gcc -o calculator calculator.o -L. -lEval -Wl,-rpath,'$ORIGIN' | ||
|
||
# change binary @rpath | ||
# install_name_tool -change "@rpath/libEval.dylib" "@executable_path/libEval.dylib" calculator | ||
|
||
file $LIBS_DIR/libEval | ||
du -h $LIBS_DIR/libEval | ||
|
||
# clean | ||
rm -rf app/c-src/*.{hi,o} | ||
rm -rf app/*.{hi,o} |
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,21 @@ | ||
{ mkDerivation, aeson, base, bytestring, HDBC, HDBC-postgresql | ||
, heroku, hpack, hspec, http-types, lib, network, network-uri | ||
, optparse-applicative, text, unordered-containers, wai, warp | ||
, fmt, random, text-show, cassava | ||
}: | ||
mkDerivation { | ||
pname = "pgrest"; | ||
version = "0.1.0.0"; | ||
src = ./.; | ||
isLibrary = false; | ||
isExecutable = true; | ||
libraryToolDepends = [ hpack ]; | ||
executableHaskellDepends = [ | ||
aeson base bytestring HDBC HDBC-postgresql heroku hspec http-types | ||
network network-uri optparse-applicative text unordered-containers | ||
wai warp fmt cassava | ||
]; | ||
prePatch = "hpack"; | ||
homepage = "https://github.com/githubuser/pgrest#readme"; | ||
license = lib.licenses.bsd3; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"r_sys","littleEndian":true,"symbols":{"test_serde":{"parameters":[],"result":"ptr","nonBlocking":false}},"typeDefs":{"A":{"b":"Vec"}},"tsTypes":{"A":"export type A = {\n b: Array<string>;\n};"}} |
Oops, something went wrong.