Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangpq authored and hoangpq committed Jan 25, 2023
1 parent ef04ff7 commit b0dff29
Show file tree
Hide file tree
Showing 342 changed files with 138,978 additions and 0 deletions.
Empty file added .Rhistory
Empty file.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.idea
/target
Cargo.lock
/cmake-build-debug
dist-newstyle/
1 change: 1 addition & 0 deletions .vscode/configurationCache.log
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":[]}}
5 changes: 5 additions & 0 deletions .vscode/dryrun.log
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'

5 changes: 5 additions & 0 deletions .vscode/settings.json
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"
}
622 changes: 622 additions & 0 deletions .vscode/targets.log

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions CHANGELOG.md
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.
12 changes: 12 additions & 0 deletions CMakeLists.txt
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)
11 changes: 11 additions & 0 deletions Cargo.toml
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" }
11 changes: 11 additions & 0 deletions Makefile
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
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# deno_r
145 changes: 145 additions & 0 deletions app/Main.hs
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
12 changes: 12 additions & 0 deletions app/Main_stub.h
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

12 changes: 12 additions & 0 deletions app/c-src/Eval_stub.h
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

24 changes: 24 additions & 0 deletions app/c-src/calculator.c
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;
}
6 changes: 6 additions & 0 deletions app/c-src/hello.c
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;
}
35 changes: 35 additions & 0 deletions app/c-src/hsbracket.c
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
7 changes: 7 additions & 0 deletions app/c-src/test.c
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..!");
}
33 changes: 33 additions & 0 deletions build-hs-dylib.sh
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}
21 changes: 21 additions & 0 deletions default.nix
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;
}
17 changes: 17 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/bankMap.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/bindings.json
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};"}}
Loading

0 comments on commit b0dff29

Please sign in to comment.