Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9018167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:27:23+00:00 2026-06-16T04:27:23+00:00

I am trying to do some OO functionality in Lua via the C API.

  • 0

I am trying to do some OO functionality in Lua via the C API. In my Lua script I have the following:

Parser = {}
Parser.__index = Parser

function Parser:create(url)
    local self = {}
    print ("creating")
    self.baseUrl = url
    setmetatable(self, Parser)
    return self
end

function Parser:Foo()
    print ("baseUrl: " .. self.baseUrl)
end

p = Parser:create("http://www.google.com")
p:Foo()

If I run this from the command line, it works fine and I see the following output:

creating
baseUrl: http://www.google.com

Now, if I comment out the last two lines and try the following through the C API

// <load the state and lua file>
lua_getglobal(L, "Parser");
lua_getfield(L, -1, "create");
lua_pushstring(L, "http://www.google.com");

if (lua_pcall(L, 1, 1, 0) != 0)
{
  // handle error
}

This works. I see “creating” in my standard output as expected. As I understand it, the new Parser object is now on top of the stack. If I them immediately try the following:

lua_getfield(L, -1, "Foo");
if (lua_pcall(L, 0, 0, 0) != 0)
{
  logger()->error("-- %1", lua_tostring(L, -1));
}

I get the following error: attempt to index local ‘self’ (a nil value)

Can anyone tell me what I’m doing wrong and how to get the function to run as expected?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:27:24+00:00Added an answer on June 16, 2026 at 4:27 am

    The definition function Parser:Foo() ... end is equivalent to:

    Parser.Foo = function(self)
        print ("baseUrl: " .. self.baseUrl)
    end
    

    That is — Foo is a function that takes one argument. When you call lua_pcall(L, 0, 0, 0) you are passing 0 arguments. Change it to lua_pcall(L, 1, 0, 0) and everything should work. (You will have to also change the pcall to create to correctly pass 2 arguments rather than 1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm loading some content via AJAX and trying to add some functionality, I have
I have a question about some functionality I'm trying to add to my jQuery
Here's what i'm trying to do: I what to have some FTP functionality in
I'm trying to embed some Lua scripting functionality in my C# app by using
I am trying to add some functionality to a page I have inherited, but
I'm trying to incorporate some Gravatar functionality using its XML-RPC API in an iPhone
I'm trying to add some functionality from a plugin I have made into a
I am trying to integrate some OpenCV functionality into my application. Currently I have
I am trying to create some functionality when a user clicks on an element
I'm trying to integrate some drawing functionality into my program. I have a JLabel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.