@@ -46,6 +46,8 @@ private function __construct()
46
46
int Tcl_Init(void *interp);
47
47
int Tcl_Eval(void *interp, const char *cmd);
48
48
const char* Tcl_GetStringResult(void *interp);
49
+ const char* Tcl_GetVar(void* interp, const char* varName, int flags);
50
+ char* Tcl_SetVar(void* interp, const char* varName, const char* newValue, int flags);
49
51
" , $ libPath );
50
52
}
51
53
@@ -66,16 +68,18 @@ public static function getInstance(): self
66
68
* Evaluates a given Tcl command.
67
69
*
68
70
* @param string $command The Tcl command to execute.
71
+ * @return mixed The result of the command if successful
69
72
* @throws \RuntimeException if the Tcl command returns an error.
70
73
*/
71
- public function evalTcl (string $ command ): void
74
+ public function evalTcl (string $ command )
72
75
{
73
76
$ interp = $ this ->getInterp ();
74
77
$ result = $ this ->ffi ->Tcl_Eval ($ interp , $ command );
75
78
if ($ result !== 0 ) { // Check for errors
76
79
$ error = $ this ->ffi ->Tcl_GetStringResult ($ interp );
77
80
throw new \RuntimeException ("Tcl Error: " . $ error );
78
81
}
82
+ return $ this ->getResult ();
79
83
}
80
84
81
85
/**
@@ -93,6 +97,45 @@ public function getResult(): string
93
97
return FFI ::string ($ result );
94
98
}
95
99
100
+ /**
101
+ * Gets the value of a Tcl variable.
102
+ *
103
+ * @param string $varName The name of the Tcl variable to get
104
+ * @return string The value of the Tcl variable
105
+ */
106
+ public function getVar (string $ varName ): string
107
+ {
108
+ $ interp = $ this ->getInterp ();
109
+ $ result = $ this ->ffi ->Tcl_GetVar ($ interp , $ varName , 0 );
110
+ if (is_string ($ result )) {
111
+ return $ result ;
112
+ }
113
+ if ($ result === null ) {
114
+ return "" ;
115
+ }
116
+ return FFI ::string ($ result );
117
+ }
118
+
119
+ /**
120
+ * Sets the value of a Tcl variable.
121
+ *
122
+ * @param string $varName The name of the Tcl variable to set
123
+ * @param string $value The new value for the variable
124
+ * @return string The new value of the variable
125
+ */
126
+ public function setVar (string $ varName , string $ value ): string
127
+ {
128
+ $ interp = $ this ->getInterp ();
129
+ $ result = $ this ->ffi ->Tcl_SetVar ($ interp , $ varName , $ value , 0 );
130
+ if (is_string ($ result )) {
131
+ return $ result ;
132
+ }
133
+ if ($ result === null ) {
134
+ return "" ;
135
+ }
136
+ return FFI ::string ($ result );
137
+ }
138
+
96
139
/**
97
140
* Returns the Tcl interpreter instance.
98
141
*
0 commit comments