简体   繁体   English

Lua / C ++从头开始绑定

[英]Lua/C++ binding from scratch

I'm new to Lua, and trying to understand some of the fundamentals. 我是Lua的新手,并试图理解一些基本原理。 Something I want to understand is binding Lua to C++ instances. 我想要理解的是将Lua绑定到C ++实例。

I am not interested in third party libraries, I want to understand this at a more fundamental level - thanks :) 我对第三方库不感兴趣,我想在更基础的层面上理解这一点 - 谢谢:)

Here are my questions: 这是我的问题:

  1. My assumption based on what I have read, is that Lua can only bind to static C functions. 我根据我所读到的假设是,Lua只能绑定到静态C函数。 Is this correct? 它是否正确?
  2. Does that mean that to bind an instance of a C++ class, I'd first need to write static functions for each method and property getter/setter I want, accepting an instance pointer as a paramter. 这是否意味着要绑定C ++类的实例,我首先需要为每个方法和我想要的属性getter / setter编写静态函数,接受实例指针作为参数。
  3. I'd register these functions with Lua. 我将这些函数注册到Lua。
  4. I'd pass Lua a pointer to the instance of the C++ class. 我将Lua指向C ++类的实例。
  5. From Lua I'd call one of the registered functions, passing the C++ instance pointer. 从Lua我调用一个已注册的函数,传递C ++实例指针。
  6. The static function dereferences the pointer, calling the equivalent method. 静态函数取消引用指针,调用等效方法。

Does this make sense? 这有意义吗? Or have I gotten something wrong? 或者我弄错了什么?

Thanks for reading this far. 感谢您阅读这篇文章。

This is right up my ally. 这是我的盟友。

1) Lua ... it doesn't really bind to stuff, what you need to do is "play nice with Lua" and that requires knowing a bit about how Lua works. 1)Lua ...它并不真正绑定到东西,你需要做的是“与Lua玩得很好”,这需要了解一下Lua是如何工作的。

I REALLY suggest reading http://luaforge.net/docman/83/98/ANoFrillsIntroToLua51VMInstructions.pdf that. 我真的建议阅读http://luaforge.net/docman/83​​/98/ANoFrillsIntroToLua51VMInstructions.pdf

That tells you about EVERYTHING Lua is actually able to do. 这告诉你Lua实际上能做的一切。 So the functions Lua gives you let you manipulate just those structures. 所以Lua给你的功能让你可以操纵那些结构。

After that everything makes a lot more sense. 之后,一切都变得更有意义。

Why this answer should end here 为什么这个答案应该在这里结束

Your questions after 1 are all wrong. 1之后你的问题都是错的。 and 1 is semantically wrong, a static function just has internal/weak linkage. 并且1在语义上是错误的,静态函数只有内部/弱连接。 I guess you mean "not a method" 我猜你的意思是“不是一种方法”

2) Not really, remember you have that nice "self"/"this" identity with objects (and lua with tables/meta-tables) - you don't bind to methods. 2)不是真的,记住你有很好的“自我”/“这个”身份与对象(和lua与表/元表) - 你不绑定到方法。

You want Lua to call some function of yours with a "self" argument, that "self" (whatever it may be, a simple integer ID, or a void* if you're feeling dangerous) should tell you what ojbect you are working with. 你想让Lua用一个“自我”参数来调用你的某些函数,即“自我”(无论它可能是什么,一个简单的整数ID,或者如果你感觉很危险的话,它都是*)应该告诉你你正在做什么ojbect用。

3/4/5/6 don't really make sense, read that document :) Comment in reply to this if you need more or have something more specific, it's not a bad question btw it's just naive 3/4/5/6真的没有意义,请阅读该文件:)如果您需要更多或更具体的东西,请回复此评论,这不是一个糟糕的问题btw它只是天真

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM