Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 483c82e

Browse files
committedSep 15, 2020
add IT support
1 parent e39cf0d commit 483c82e

File tree

6 files changed

+80
-28
lines changed

6 files changed

+80
-28
lines changed
 

‎.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
# Wasm files
55
*.wasm
66
*.wat
7+
8+
# REPL history
9+
.repl_history

‎Config.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
modules_dir = "./"
2+
3+
[[module]]
4+
name = "sqlite3"
5+
mem_pages_count = 100
6+
logger_enabled = true

‎Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ LDFLAGS = -Wl,--no-entry,--demangle,--allow-undefined
77
EXPORT_FUNCS = \
88
--export=allocate,$\
99
--export=deallocate,$\
10-
--export=invoke,$\
11-
--export=load,$\
12-
--export=store
10+
--export=set_result_size,$\
11+
--export=set_result_ptr,$\
12+
--export=get_result_size,$\
13+
--export=get_result_ptr,$\
14+
--export=invoke
1315
SQLITE_SRC = \
1416
src/alter.c\
1517
src/analyze.c\

‎sdk/logger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define FLUENCE_C_SDK_LOGGER_H
33

44
#define __LOGGER_IMPORT(name) \
5-
__attribute__((__import_module__("logger"), __import_name__(#name)))
5+
__attribute__((__import_module__("host"), __import_name__(#name)))
66

77
/**
88
* Writes provided utf8 string to Wasm VM logger.

‎sqlite3.wit

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
;; Fluence SQLite fork Wasm Interface Types
2+
3+
;; allocate
4+
(@interface type (func (param i32) (result i32))) ;; 0
5+
6+
;; deallocate
7+
(@interface type (func (param i32 i32))) ;; 1
8+
9+
;; invoke
10+
(@interface type (func (param string) (result string))) ;; 2
11+
12+
;; get_result_ptr/get_result_size
13+
(@interface type (func (result i32))) ;; 3
14+
15+
;; set_result_ptr/set_result_size
16+
(@interface type (func (param i32))) ;; 4
17+
18+
19+
(@interface export "allocate" (func 0)) ;; 0
20+
(@interface export "deallocate" (func 1)) ;; 1
21+
(@interface export "invoke" (func 2)) ;; 2
22+
(@interface export "get_result_size" (func 3)) ;; 3
23+
(@interface export "get_result_ptr" (func 3)) ;; 4
24+
(@interface export "set_result_size" (func 4)) ;; 5
25+
(@interface export "set_result_ptr" (func 4)) ;; 6
26+
27+
;; adapter for export invoke function
28+
(@interface func (type 2)
29+
arg.get 0
30+
string.size
31+
call-core 0 ;; call allocate
32+
arg.get 0
33+
string.lower_memory
34+
call-core 2 ;; call invoke
35+
call-core 4 ;; call get_result_size
36+
call-core 3 ;; call get_result_ptr
37+
string.lift_memory
38+
call-core 4 ;; call get_result_size
39+
call-core 3 ;; call get_result_ptr
40+
call-core 1 ;; call deallocate
41+
)
42+
43+
;; Implementations
44+
(@interface implement (func 2) (func 2))

‎src/wrapper.c

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "sqliteInt.h"
44

55
sqlite3 *state;
6+
char *RESULT_PTR;
7+
int RESULT_SIZE;
68

79
int init() {
810
const int rc = sqlite3_initialize();
@@ -15,14 +17,6 @@ int init() {
1517

1618
int g_isInited = 0;
1719

18-
void store(char *ptr, unsigned char byte) {
19-
*ptr = byte;
20-
}
21-
22-
unsigned char load(const unsigned char *ptr) {
23-
return *ptr;
24-
}
25-
2620
void* allocate(size_t size) {
2721
return malloc(size + 1);
2822
}
@@ -31,15 +25,20 @@ void deallocate(void *ptr, int size) {
3125
free(ptr);
3226
}
3327

34-
char *write_response(char *response, int response_size) {
35-
char *result_response = allocate(response_size + 4);
28+
void set_result_ptr(char *ptr) {
29+
RESULT_PTR = ptr;
30+
}
3631

37-
for(int i = 0; i < 4; ++i) {
38-
result_response[i] = (response_size >> 8*i) & 0xFF;
39-
}
32+
void set_result_size(int size) {
33+
RESULT_SIZE = size;
34+
}
35+
36+
int get_result_size(void) {
37+
return RESULT_SIZE;
38+
}
4039

41-
memcpy(result_response + 4, response, response_size);
42-
return result_response;
40+
char *get_result_ptr() {
41+
return RESULT_PTR;
4342
}
4443

4544
typedef struct ShellText ShellText;
@@ -114,7 +113,7 @@ static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
114113
return 0;
115114
}
116115

117-
const char *invoke(char *request, int request_size) {
116+
void invoke(char *request, int request_size) {
118117
if(g_isInited == 0) {
119118
// TODO: check the return code
120119
init();
@@ -141,19 +140,17 @@ const char *invoke(char *request, int request_size) {
141140

142141
char *response = 0;
143142
if(rc || errorMessage) {
144-
response = write_response(errorMessage, strlen(errorMessage));
143+
RESULT_PTR = errorMessage;
144+
RESULT_SIZE = strlen(errorMessage);
145145
}
146146
else {
147147
if(str.n != 0) {
148-
response = write_response(str.z, str.n);
148+
RESULT_PTR = str.z;
149+
RESULT_SIZE = str.n;
149150
} else {
150151
// if a request was successfull, sqlite doesn't return anything as the result string
151-
const char success_result[] = "OK";
152-
response = write_response((char *)success_result, strlen(success_result));
152+
RESULT_PTR = strdup("OK");
153+
RESULT_SIZE = 2;
153154
}
154155
}
155-
156-
freeText(&str);
157-
158-
return response;
159156
}

0 commit comments

Comments
 (0)
Please sign in to comment.