A ctor which receives const char*

51 views
Skip to first unread message

Ronny Nissengold

unread,
Aug 6, 2023, 8:20:59 AM8/6/23
to emscripten-discuss
Hi all,
I am trying to wrap my code with embind, and I have a ctor which receives 3 in params of const char*.
So I wrote the following:
And got an error that says that  PKc  is not supported:
Cannot construct  foo   due to unbound types: PKc

How can I rewrite the embinding please? I cannot change the CTOR function.

EMSCRIPTEN_BINDINGS(contextTest) {
emscripten::class_<Foo>("foo").
constructor<const char*, const char*, const char*>();
}

Shlomi Fish

unread,
Aug 7, 2023, 1:03:35 AM8/7/23
to Ronny Nissengold, emscripte...@googlegroups.com
Hi Ronny,
please go over https://github.com/shlomif/how-to-share-code-online and follow
its advice.

--

Shlomi Fish https://www.shlomifish.org/
Beginners Site for the Vim text editor - https://vim.begin-site.org/

The NSA knows what you did last summer. But no one, in the NSA or outside it,
knows why they should.
https://www.shlomifish.org/humour/bits/facts/NSA/

Please reply to list if it's a mailing list post - https://shlom.in/reply .

Ronny Nissengold

unread,
Aug 7, 2023, 3:41:48 AM8/7/23
to emscripten-discuss
Here is the full compiling cpp code:

#include <string>
#include <emscripten/bind.h>
#include <emscripten/val.h>

class Foo
{
  public:
      Foo(const char* a, const char* b, const char* c)
      {
         m_a = std::string(a);
         m_b = std::string(b);
         m_c = std::string(c);
      }

  private:
      std::string m_a;
      std::string m_b;
      std::string m_c;
};


EMSCRIPTEN_BINDINGS(FooTest)
{
    emscripten::class_<Foo>("Foo").

    constructor<const char*, const char*, const char*>();
}

ב-יום שני, 7 באוגוסט 2023 בשעה 08:03:35 UTC+3, Shlomi Fish כתב/ה:

Mike Lischke

unread,
Aug 7, 2023, 5:25:13 AM8/7/23
to emscripte...@googlegroups.com
Here is the full compiling cpp code:

#include <string>
#include <emscripten/bind.h>
#include <emscripten/val.h>

class Foo
{
  public:
      Foo(const char* a, const char* b, const char* c)
      {
         m_a = std::string(a);
         m_b = std::string(b);
         m_c = std::string(c);
      }

  private:
      std::string m_a;
      std::string m_b;
      std::string m_c;
};


EMSCRIPTEN_BINDINGS(FooTest)
{
    emscripten::class_<Foo>("Foo").
    constructor<const char*, const char*, const char*>();
}

ב-יום שני, 7 באוגוסט 2023 בשעה 08:03:35 UTC+3, Shlomi Fish כתב/ה:

Is this supposed to be called from JS? If so, how can you provide char pointers for the constructor, given that you only have JS strings?

Instead use std::string for the constructor. If you cannot change that write a helper class which takes strings and passes the data() pointer along to the inherited c-tor. Alternatively, you can write a constructor factory method, known as external constructor (https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html#external-constructors).


Ronny Nissengold

unread,
Aug 7, 2023, 7:14:55 AM8/7/23
to emscripten-discuss
Thanks a lot Mike!
Looks like I will inherit a CTOR and use it with std::string

ב-יום שני, 7 באוגוסט 2023 בשעה 12:25:13 UTC+3, mike.l...@googlemail.com כתב/ה:
Reply all
Reply to author
Forward
0 new messages