-
Notifications
You must be signed in to change notification settings - Fork 0
/
foundation-0.0.26.1.patch
104 lines (101 loc) · 3.12 KB
/
foundation-0.0.26.1.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
diff --git b/foundation.cabal a/foundation.cabal
index 224c61308..ef25e37f8 100644
--- b/foundation.cabal
+++ a/foundation.cabal
@@ -1,3 +1,4 @@
+cabal-version: 2.0
name: foundation
version: 0.0.26.1
synopsis: Alternative prelude with batteries and no dependencies
@@ -28,7 +29,6 @@ category: foundation
build-type: Simple
homepage: https://github.com/haskell-foundation/foundation
bug-reports: https://github.com/haskell-foundation/foundation/issues
-cabal-version: 1.18
extra-source-files: cbits/*.h
source-repository head
@@ -190,13 +190,17 @@ library
if os(osx)
exposed-modules: Foundation.System.Bindings.Macos
+ if os(ghcjs)
+ js-sources: jsbits/bindings.js
+ extra-bundled-libraries: EMCCfoundation EMCCfoundation.exported
+
default-extensions: NoImplicitPrelude
RebindableSyntax
TypeFamilies
BangPatterns
DeriveDataTypeable
- if impl(ghc < 8.0) || impl(ghcjs)
+ if impl(ghc < 8.0)
buildable: False
else
build-depends: base
diff --git b/jsbits/bindings.js a/jsbits/bindings.js
new file mode 100644
index 000000000..fc51a85fe
--- /dev/null
+++ a/jsbits/bindings.js
@@ -0,0 +1,61 @@
+
+// EMCC:EXPORTED_FUNCTIONS _malloc _free
+// EMCC:EXPORTED_FUNCTIONS _foundation_rngV1_generate
+
+function h$foundation_sysrandom_linux(buf_d, buf_o, size) {
+ return -19; // ENODEV; foundation returns the same for non-linux hosts.
+}
+
+// c_rngv1_generate :: Ptr Word8 -- new key
+// -> Ptr Word8 -- destination
+// -> Ptr Word8 -- current key
+// -> CountOf Word8 -- number of bytes to generate
+// -> IO Word32
+//
+// int foundation_rngV1_generate(uint8_t newkey[CHACHA_KEY_SIZE], uint8_t *dst, uint8_t key[CHACHA_KEY_SIZE], FsCountOf bytes)
+//
+// #define CHACHA_KEY_SIZE 3
+
+function h$copyToHeap(buf_d, buf_o, tgt, len) {
+ if(len === 0) return;
+ var u8 = buf_d.u8;
+ for(var i=0;i<len;i++) {
+ HEAPU8[tgt+i] = u8[buf_o+i];
+ }
+}
+
+function h$copyFromHeap(src, buf_d, buf_o, len) {
+ var u8 = buf_d.u8;
+ for(var i=0;i<len;i++) {
+ u8[buf_o+i] = HEAPU8[src+i];
+ }
+}
+
+function h$withCBuffer(str_d, str_o, len, cont) {
+ var str = _malloc(len);
+ if(str_d !== null) h$copyToHeap(str_d, str_o, str, len);
+ var ret = cont(str);
+ _free(str);
+ return ret;
+}
+
+function h$withOutBuffer(ptr_d, ptr_o, len, cont) {
+ var ptr = _malloc(len);
+ h$copyToHeap(ptr_d, ptr_o, ptr, len);
+ var ret = cont(ptr);
+ h$copyFromHeap(ptr, ptr_d, ptr_o, len);
+ _free(ptr);
+ return ret;
+ }
+
+
+var CHACHA_KEY_SIZE = 32;
+function h$foundation_rngV1_generate(newkey_d, newkey_o, dst_d, dst_o, key_d, key_o, dst_len) {
+ return h$withCBuffer(newkey_d, newkey_o, CHACHA_KEY_SIZE, function(newkey) {
+ return h$withOutBuffer(dst_d, dst_o, dst_len, function(dst) {
+ return h$withCBuffer(key_d, key_o, CHACHA_KEY_SIZE, function(key) {
+ return _foundation_rngV1_generate(newkey, dst, key, dst_len);
+ });
+ });
+ });
+}
\ No newline at end of file