arcticfox1919/LuaDardo

Can I expand to support SciDart?

polotto opened this issue · 5 comments

Amazing project, congratulations!

I have been searching for some interpreter written in Dart and so far your implementation is the best that I found!!!
I created a library called SciDart(https://scidart.org/) my intention is create a full and functional Scientific platform for Dart and to achieve this I need an interpreter. Can I use your implementation and extend to support SciDart?

@polotto
Of course! It's just that LuaDardo is still experimental. I don't have time to optimize and continue to develop the Lua standard library. It is not recommended to use it in release projects.

Thank you! The SciDart is experimental as well, I think we are good.

Can you answer a question?
I trying bind SciDart with LuaDardo and I face this problem:

static int _sayHi(LuaState ls) {
    Person p = ls.toUserdata<Person>(1).data;
    if (ls.isString(-1)) {
     var result = p.sayHi(ls.toStr(-1));
     // How do I put this result back to Lua code?
    }
    return 0;
  }

How do I put the result that I get in Dart back to Lua?

@polotto

Dart interacts with Lua through a stack, maybe you can take a look at the Lua C API guide, LuaDardo is also compatible with these APIs. In addition, README also adds some examples.

An example:

import 'package:lua_dardo/lua.dart';
import 'dart:math';

//  wrapper function must use this signature:int Function(LuaState ls)
int randomInt(LuaState ls) {
  int max = ls.checkInteger(1);
  ls.pop(1);

  var random = Random();
  var randVal = random.nextInt(max);
  ls.pushInteger(randVal);
  return 1;
}

void main(List<String> arguments) {
  LuaState state = LuaState.newState();
  state.openLibs();

  state.pushDartFunction(randomInt);
  state.setGlobal('randomInt');

  // execute the Lua script to test the randomInt function
  state.loadString('''
rand_val = randomInt(10)
print('random value is '..rand_val)
''');
  state.call(0, 0);
}

Thank you! Is there some way to generate these binds automatically? That would make the integration with my library more easy.