Permalink
Browse files

Proof of concept. Duktape engine linked in and echo command implemented.

  • Loading branch information...
dulsi committed Oct 26, 2016
1 parent 13a3f84 commit 593583040cb9e44a17e3be84fea2411b97bf8fbc
Showing with 53 additions and 0 deletions.
  1. +9 −0 init.lua
  2. +44 −0 scriptmine.c
View
@@ -0,0 +1,9 @@
require("scriptmine")
minetest.register_chatcommand("js", {
params = "<command>",
description = "Run javascript command",
func = function(name, param)
javascript(name, param);
end,
})
View
@@ -0,0 +1,44 @@
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "duktape.h"
duk_context *ctx = NULL;
lua_State *Lg = NULL;
const char *name = NULL;
duk_ret_t echo(duk_context *ctx)
{
const char *text = duk_to_lstring(ctx, 0 /*index*/, NULL);
lua_getfield(Lg, LUA_GLOBALSINDEX, "minetest");
lua_getfield(Lg, -1, "chat_send_player");
lua_remove(Lg, -2);
lua_pushstring(Lg, name);
lua_pushstring(Lg, text);
lua_pushboolean(Lg, 0);
lua_call(Lg, 3, 1);
return 1;
}
static int javascript(lua_State *L)
{
const char *cmd = lua_tostring(L, -1);
name = lua_tostring(L, -2);
Lg = L;
duk_eval_string(ctx, cmd);
return 1;
}
int luaopen_scriptmine(lua_State *L)
{
ctx = duk_create_heap_default();
duk_push_global_object(ctx);
duk_push_c_function(ctx, echo, 1 /*nargs*/);
duk_put_prop_string(ctx, -2, "echo");
if (!ctx) {
printf("Failed to create a Duktape heap.\n");
exit(1);
}
lua_register(L, "javascript", javascript);
return 0;
}

0 comments on commit 5935830

Please sign in to comment.