Skip to content

Commit 154d7f7

Browse files
Add Tcl variable management methods to ProcessTCL class
1 parent 760d442 commit 154d7f7

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/ProcessTCL.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ private function __construct()
4646
int Tcl_Init(void *interp);
4747
int Tcl_Eval(void *interp, const char *cmd);
4848
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);
4951
", $libPath);
5052
}
5153

@@ -66,16 +68,18 @@ public static function getInstance(): self
6668
* Evaluates a given Tcl command.
6769
*
6870
* @param string $command The Tcl command to execute.
71+
* @return mixed The result of the command if successful
6972
* @throws \RuntimeException if the Tcl command returns an error.
7073
*/
71-
public function evalTcl(string $command): void
74+
public function evalTcl(string $command)
7275
{
7376
$interp = $this->getInterp();
7477
$result = $this->ffi->Tcl_Eval($interp, $command);
7578
if ($result !== 0) { // Check for errors
7679
$error = $this->ffi->Tcl_GetStringResult($interp);
7780
throw new \RuntimeException("Tcl Error: " . $error);
7881
}
82+
return $this->getResult();
7983
}
8084

8185
/**
@@ -93,6 +97,45 @@ public function getResult(): string
9397
return FFI::string($result);
9498
}
9599

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+
96139
/**
97140
* Returns the Tcl interpreter instance.
98141
*

0 commit comments

Comments
 (0)