Tcl & Tk/확장 패키지 (Extension Package)
tcl-duktape 0.12.1
티클러
2025. 8. 18. 09:49
홈페이지: https://github.com/dbohdan/tcl-duktape
https://wiki.tcl-lang.org/page/tcl-duktape
이 Tcl 확장은 자바스크립트 인터프리터 라이브러리인 Duktape에 대한 바인딩을 제공합니다.
This Tcl extension provides bindings for Duktape, a JavaScript interpreter library.
#!/usr/bin/env tclsh
package require duktape
package require duktape::oo
set duktapeObj [::duktape::oo::Duktape new]
$duktapeObj jsproc ::add {{a 0 number} {b 0 number}} {
return a + b;
}
puts [add 1 2]
$duktapeObj jsmethod cos {{deg 0 number}} {
return Math.cos(deg * Math.PI / 180);
}
puts [$duktapeObj cos 360]
$duktapeObj destroy
#! /usr/bin/env tclsh
package require duktape 0.3.0
package require duktape::oo
package require fileutil
package require http
set dt [::duktape::oo::Duktape new]
# Download chess.js if necessary.
if {![file exists chess.js]} {
set req [::http::geturl http://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js]
set c [::http::data $req]
::http::cleanup $req
::fileutil::writeFile chess.js $c
}
# Set up the game.
$dt eval [::fileutil::cat chess.js]
$dt eval {
function Game() {
'use strict';
const game = {};
game.chess = new Chess();
game.next_move = function () {
if (game.chess.game_over()) {
return null;
} else {
const moves = game.chess.moves();
const move = moves[Math.floor(Math.random()*moves.length)];
game.chess.move(move);
return move;
};
};
return game;
};
const game1 = Game();
}
# Play.
while 1 {
set move [$dt call-method-str game1.next_move undefined]
if {$move eq {null}} break
puts $move
}