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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use std::borrow::Cow;
use std::ffi::{CStr, CString};
use std::path::Path;
use std::ptr;
use ll::*;
use tcl::{TclResult, TclEnvironment};
use object::{Object, TclObject};
pub enum EvalScope {
Local = 0,
Global = TCL_EVAL_GLOBAL as isize,
}
pub enum ByteCompile {
Compile = 0,
Direct = TCL_EVAL_DIRECT as isize
}
pub enum SetVariableScope {
Standard = 0,
GlobalOnly = TCL_GLOBAL_ONLY as isize,
NamespaceOnly = TCL_NAMESPACE_ONLY as isize,
}
pub enum GetVariableScope {
Standard = 0,
GlobalOnly = TCL_GLOBAL_ONLY as isize,
}
pub enum LeaveError {
No = 0,
Yes = TCL_LEAVE_ERR_MSG as isize
}
pub enum AppendStyle {
Replace = 0,
Append = TCL_APPEND_VALUE as isize,
ReplaceAsList = TCL_LIST_ELEMENT as isize,
AppendAsList = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT) as isize
}
pub struct Interpreter<'env> {
_env: &'env TclEnvironment,
raw: *mut Tcl_Interp
}
impl <'env> Drop for Interpreter<'env> {
fn drop(&mut self) {
unsafe { Tcl_DeleteInterp(self.raw) };
}
}
impl<'env> Interpreter<'env> {
pub fn new(env: &TclEnvironment) -> Result<Interpreter, &str> {
unsafe {
let raw = Tcl_CreateInterp();
if raw == ptr::null_mut() {
Err("Failed to create interpreter")
} else {
if Tcl_Init(raw) != TCL_OK {
Err("Couldn't initialize interpreter")
} else {
Ok(Interpreter {
_env: env,
raw: raw
})
}
}
}
}
pub unsafe fn raw(&mut self) -> *mut Tcl_Interp {
self.raw
}
pub fn is_safe(&self) -> bool {
unsafe { Tcl_IsSafe(self.raw) == 1 }
}
pub fn make_safe(&mut self) -> TclResult {
let result = unsafe { Tcl_MakeSafe(self.raw) };
TclResult::from_ll(result, self)
}
pub fn string_result(&self) -> Cow<str> {
unsafe {
let string = Tcl_GetStringResult(self.raw);
String::from_utf8_lossy(CStr::from_ptr(string).to_bytes())
}
}
pub fn object_result(&self) -> Object<'env> {
unsafe {
let object = Tcl_GetObjResult(self.raw);
Object::from_raw(self._env, object)
}
}
pub fn eval_file(&mut self, path: &Path) -> TclResult {
let buf = CString::new(path.to_string_lossy().as_bytes()).unwrap().as_ptr();
let result = unsafe {
Tcl_EvalFile(self.raw, buf)
};
TclResult::from_ll(result, self)
}
pub fn eval(&mut self, code: &str, eval_scope: EvalScope) -> TclResult {
let buf = CString::new(code.as_bytes()).unwrap().as_ptr();
let flags = eval_scope as i32;
let result = unsafe {
Tcl_EvalEx(self.raw, buf, code.len() as i32, flags)
};
TclResult::from_ll(result, self)
}
pub fn eval_object(&mut self, code: &Object, eval_scope: EvalScope, byte_compile: ByteCompile) -> TclResult {
let flags = (eval_scope as i32) | (byte_compile as i32);
let result = unsafe {
Tcl_EvalObjEx(self.raw, code.raw(), flags)
};
TclResult::from_ll(result, self)
}
pub fn list_append(&mut self, target: &mut Object, source: &Object) -> TclResult {
let result = unsafe {
Tcl_ListObjAppendElement(self.raw, target.raw(), source.raw())
};
TclResult::from_ll(result, self)
}
pub fn expression_boolean<'a>(&'a mut self, expr: &str) -> Result<bool, Cow<'a, str>> {
let mut output = 0;
let buf = CString::new(expr.as_bytes()).unwrap().as_ptr();
unsafe {
if Tcl_ExprBoolean(self.raw, buf, &mut output) == TCL_OK {
Ok(output == 1)
} else {
Err(self.string_result())
}
}
}
pub fn expression_boolean_from_object<'a>(&'a mut self, expr: &Object) -> Result<bool, Cow<'a, str>> {
let mut output = 0;
unsafe {
if Tcl_ExprBooleanObj(self.raw, expr.raw(), &mut output) == TCL_OK {
Ok(output == 1)
} else {
Err(self.string_result())
}
}
}
pub fn expression_double<'a>(&'a mut self, expr: &str) -> Result<f64, Cow<'a, str>> {
let mut output = 0.0;
let buf = CString::new(expr.as_bytes()).unwrap().as_ptr();
unsafe {
if Tcl_ExprDouble(self.raw, buf, &mut output) == TCL_OK {
Ok(output)
} else {
Err(self.string_result())
}
}
}
pub fn expression_double_from_object<'a>(&'a mut self, expr: &Object) -> Result<f64, Cow<'a, str>> {
let mut output = 0.0;
unsafe {
if Tcl_ExprDoubleObj(self.raw, expr.raw(), &mut output) == TCL_OK {
Ok(output)
} else {
Err(self.string_result())
}
}
}
pub fn expression_long<'a>(&'a mut self, expr: &str) -> Result<i64, Cow<'a, str>> {
let mut output = 0;
let buf = CString::new(expr.as_bytes()).unwrap().as_ptr();
unsafe {
if Tcl_ExprLong(self.raw, buf, &mut output) == TCL_OK {
Ok(output)
} else {
Err(self.string_result())
}
}
}
pub fn expression_long_from_object<'a>(&'a mut self, expr: &Object) -> Result<i64, Cow<'a, str>> {
let mut output = 0;
unsafe {
if Tcl_ExprLongObj(self.raw, expr.raw(), &mut output) == TCL_OK {
Ok(output)
} else {
Err(self.string_result())
}
}
}
pub fn expression_object_from_object<'a>(&'a mut self, expr: &Object) -> Result<Object<'env>, Cow<'a, str>> {
let mut output = ptr::null_mut();
unsafe {
if Tcl_ExprObj(self.raw, expr.raw(), &mut output) == TCL_OK {
Ok(Object::from_raw(self._env, output))
} else {
Err(self.string_result())
}
}
}
pub fn expression_string(&mut self, expr: &str) -> TclResult {
let buf = CString::new(expr.as_bytes()).unwrap().as_ptr();
let result = unsafe {
Tcl_ExprString(self.raw, buf)
};
TclResult::from_ll(result, self)
}
pub fn set_string_variable(&mut self, var_name: &str, new_value: &str,
scope: SetVariableScope, leave_error: LeaveError, append_style: AppendStyle) -> String {
let flags = scope as i32 | leave_error as i32 | append_style as i32;
let var_buf = CString::new(var_name.as_bytes()).unwrap().as_ptr();
let val_buf = CString::new(new_value.as_bytes()).unwrap().as_ptr();
unsafe {
let result = Tcl_SetVar(self.raw, var_buf, val_buf, flags);
String::from_utf8_lossy(CStr::from_ptr(result).to_bytes()).to_string()
}
}
pub fn set_variable<V: TclObject>(&mut self, var_name: &str, new_value: V)
-> Option<Object<'env>> {
self.set_object_variable(
&Object::new(self._env, var_name), &Object::new(self._env, new_value),
SetVariableScope::Standard,
LeaveError::No,
AppendStyle::Replace
)
}
pub fn set_object_variable(&mut self, var_name: &Object, new_value: &Object,
scope: SetVariableScope, leave_error: LeaveError, append_style: AppendStyle) -> Option<Object<'env>> {
let flags = scope as i32 | leave_error as i32 | append_style as i32;
unsafe {
let result = Tcl_ObjSetVar2(self.raw, var_name.raw(), ptr::null_mut(), new_value.raw(), flags);
if result == ptr::null_mut() {
None
} else {
Some(Object::from_raw(self._env, result))
}
}
}
pub fn get_variable(&mut self, var_name: &str, scope: GetVariableScope, leave_error: LeaveError) -> Option<String> {
let flags = scope as i32 | leave_error as i32;
let var_buf = CString::new(var_name.as_bytes()).unwrap().as_ptr();
unsafe {
let result = Tcl_GetVar(self.raw, var_buf, flags);
if result != ptr::null() {
Some(String::from_utf8_lossy(CStr::from_ptr(result).to_bytes()).to_string())
} else {
None
}
}
}
pub fn get_object_variable(&mut self, var_name: &str, scope: GetVariableScope, leave_error: LeaveError) -> Option<Object<'env>> {
let flags = scope as i32 | leave_error as i32;
let var_buf = CString::new(var_name.as_bytes()).unwrap().as_ptr();
unsafe {
let result = Tcl_GetVar2Ex(self.raw, var_buf, ptr::null_mut(), flags);
if result == ptr::null_mut() {
None
} else {
Some(Object::from_raw(self._env, result))
}
}
}
pub fn unset_variable(&mut self, var_name: &str, scope: GetVariableScope, leave_error: LeaveError) -> TclResult {
let flags = scope as i32 | leave_error as i32;
let buf = CString::new(var_name.as_bytes()).unwrap().as_ptr();
let result = unsafe {
Tcl_UnsetVar(self.raw, buf, flags)
};
TclResult::from_ll(result, self)
}
}