forked from cyanogilvie/tcc4tcl
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.tcl
More file actions
executable file
·243 lines (207 loc) · 5.36 KB
/
test.tcl
File metadata and controls
executable file
·243 lines (207 loc) · 5.36 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#! /usr/bin/env tclsh
lappend auto_path [lindex $argv 0]
package require tcc4tcl
catch {console show}
tcc4tcl::cproc test {int i} int { return(i+42); }
tcc4tcl::cproc test1 {int i} int { return(i+42); }
tcc4tcl::cproc ::bob::test1 {int i} int { return(i+42); }
# This will fail
catch {
tcc4tcl::cproc test2 {int i} int { badcode; }
}
# This should work
tcc4tcl::cproc test3 {int i} int { return(i+42); }
# Multiple arguments
tcc4tcl::cproc add {int a int b} int { return(a+b); }
# Add external functions
tcc4tcl::cproc mkdir {Tcl_Interp* interp char* dir} ok {
int mkdir_ret;
mkdir_ret = mkdir(dir);
if (mkdir_ret != 0) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("failed", -1));
return(TCL_ERROR);
};
return(TCL_OK);
}
# Return error on NULL
tcc4tcl::cproc test4 {int v} char* {
if (v == 1) {
return("ok");
}
return(NULL);
}
puts [test 1]
puts [test1 1]
puts [test3 1]
puts [::bob::test1 1]
puts [add [test 1] 1]
puts [test4 1]
catch {
puts [mkdir "/"]
} err
if {$err != "failed"} {
error "\[mkdir\] did not return the expected error"
}
catch {
set v 0
puts [test4 0]
set v 1
} err
if {$err != "" || $v == 1} {
error "\[test4\] did not return the expected error"
}
# New API
## Test processing the commandline
set handle [tcc4tcl::new]
$handle process_command_line -Dx=1234
$handle cproc test13 {int i} int {
return(i+x);
}
$handle go
puts "[test13 1] = 1235"
## Simple test
set handle [tcc4tcl::new]
$handle cproc test5 {int i} int { return(i + 42); }
if {[$handle code] == ""} {
error "[list $handle code] did not give code output"
}
$handle cproc test6 {int i} int { return(i + 42); }
$handle go
puts [test5 1]
puts [test6 1]
## Delete without performing
set handle [tcc4tcl::new]
$handle delete
# External functions
if {[info exists ::env(TCC4TCL_TEST_RUN_NATIVE)]} {
set handle [tcc4tcl::new]
$handle cwrap curl_version {} vstring
$handle add_library_path [::tcl::pkgconfig get libdir,runtime]
$handle add_library_path /usr/lib/x86_64-linux-gnu
$handle add_library_path /usr/lib64
$handle add_library_path /usr/lib
$handle add_library_path /usr/lib32
$handle add_library curl
$handle go
puts [curl_version]
}
# wide values
set handle [tcc4tcl::new]
$handle cproc wideTest {Tcl_WideInt x} Tcl_WideInt {
return(x);
}
$handle go
puts [wideTest 30]
# Produce a loadable object
## Currently doesn't work on Darwin
if {false && [info exists ::env(TCC4TCL_TEST_RUN_NATIVE)] && $::tcl_platform(os) != "Darwin"} {
set tmpfile "/tmp/DELETEME_tcc4tcl_test_exec[expr rand()].so"
file delete $tmpfile
set handle [tcc4tcl::new $tmpfile "myPkg 0.1"]
$handle cproc ext_add {int a int b} long { return(a+b); }
$handle add_include_path [::tcl::pkgconfig get includedir,runtime]
$handle add_library_path [::tcl::pkgconfig get libdir,runtime]
$handle add_library_path /usr/lib/x86_64-linux-gnu
$handle add_library_path /usr/lib64
$handle add_library_path /usr/lib
$handle add_library_path /usr/lib32
$handle add_library tclstub${::tcl_version}
$handle go
load $tmpfile myPkg
puts [ext_add 1 42]
file delete $tmpfile
}
# More involved test
if {[info exists ::env(TCC4TCL_TEST_RUN_NATIVE)]} {
set handle [tcc4tcl::new]
$handle ccode {
#include <stdint.h>
#include <curl/curl.h>
}
$handle cwrap curl_version {} vstring
$handle cproc curl_fetch {char* url} ok {
void *handle;
handle = curl_easy_init();
if (!handle) {
return(TCL_ERROR);
}
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_perform(handle);
return(TCL_OK);
}
$handle add_include_path /usr/include
$handle add_library_path [::tcl::pkgconfig get libdir,runtime]
$handle add_library_path /usr/lib/x86_64-linux-gnu
$handle add_library_path /usr/lib64
$handle add_library_path /usr/lib
$handle add_library_path /usr/lib32
$handle add_library curl
$handle go
curl_fetch http://rkeene.org/
}
set handle [tcc4tcl::new]
$handle proc callToTcl {Tcl_Interp* ip int a int b} int {
set retval [expr {$a + $b}]
return $retval
}
$handle cwrap callToTcl {Tcl_Interp* ip int a int b} int
$handle go
if {[callToTcl 3 5] != 8} {
error "3 + 5 is 8, not [callToTcl 3 5]"
}
set handle [tcc4tcl::new]
$handle proc callToTcl1 {int x} float {
return 0.1
}
$handle cwrap callToTcl1 {int x} float
$handle go
puts [callToTcl1 3]
set handle [tcc4tcl::new]
$handle proc callToTclBinary {char* blob int blob_Length} ok {
puts "Blob: $blob ([string length $blob])"
}
$handle cproc callToTclBinaryWrapper {} void {
callToTclBinary("test\x00test", 9);
}
puts [$handle code]
$handle go
callToTclBinaryWrapper
set handle [tcc4tcl::new]
$handle cproc testClientData {int y} {int} [concat "int x = 3;" {
return(x + y);
}]
$handle go
set testVal [testClientData 1]
if {$testVal != "4"} {
error "\[ClientData\] Invalid value: $testVal, should have been 4"
}
# set handle [tcc4tcl::new]
# $handle cproc testClientData {ClientData _x=3 int y} {int} {
# int x
#
# Tcl_GetIntFromObj(NULL, _x, &x);
#
# return(x + y);
# }
# set testVal [testClientData 1]
# if {$testVal != "4"} {
# error "\[ClientData\] Invalid value: $testVal, should have been 4"
# }
set handle [tcc4tcl::new]
$handle ccommand testCCommand {dummy ip objc objv} {
Tcl_SetObjResult(ip, Tcl_NewStringObj("OKAY", 4));
return(TCL_OK);
}
$handle go
if {[testCCommand] ne "OKAY"} {
error "\[testCCommand\] Invalid result"
}
# Critcl test
package require -exact critcl 0
critcl::ccode {
#define test 1234
}
critcl::cproc test14 {int x} int {
return(x + test);
}
puts "Test14: [test14 3]"