Skip to content

IDL generator with gluecode failed std::vector #25679

@Manuelbaun

Description

@Manuelbaun

I am trying to generate the glue code for the cpp lib (libavoid), and it uses std::vector. I extracted the main issue and made a simple version of it. See the code blow. Basically, a class method expects const std::vector<MyClass>& items.

I updated the idl to expect a sequence<MyClass>. When trying to compile it throws an error, because the generated glue code emscripten_bind_MyContainer_setItems_1 takes a raw pointer, but the method is expecting a std::vector.

What I am missing, how to properly generate the sequence or std::vector?

Version of emscripten/emsdk:
version 3.1.38, 3.1.74, 4.0.18

Failing command line in full:

Full link command and output with -v appended:

#pragma once
#include <string>
#include <vector>

class MyClass {
public:
  MyClass();
  MyClass(std::string name);
  std::string getName() const;
  void setName(const std::string &n);
private:
  std::string name_;
};


class MyContainer {
public:
  std::vector<MyClass> getItems() const;
  void setItems(const std::vector<MyClass> &items);
private:
  std::vector<MyClass> items_;
};


std::vector<MyClass> MyContainer::getItems() const {
  return items_;
}

void MyContainer::setItems(const std::vector<MyClass>& items) {
  items_ = items;
}
interface MyClass {
  void MyClass();
  void MyClass(DOMString name);
  attribute DOMString name;
};

interface MyContainer {
  [Value] sequence<MyClass> getItems();
  void setItems([Ref] sequence<MyClass> items);
};

The generated glue code

#include <emscripten.h>
#include <stdlib.h>

EM_JS_DEPS(webidl_binder, "$intArrayFromString,$UTF8ToString,$alignMemory,$addOnInit");

extern "C" {

// Define custom allocator functions that we can force export using
// EMSCRIPTEN_KEEPALIVE.  This avoids all webidl users having to add
// malloc/free to -sEXPORTED_FUNCTIONS.
EMSCRIPTEN_KEEPALIVE void webidl_free(void* p) { free(p); }
EMSCRIPTEN_KEEPALIVE void* webidl_malloc(size_t len) { return malloc(len); }


// Interface: VoidPtr


void EMSCRIPTEN_KEEPALIVE emscripten_bind_VoidPtr___destroy___0(void** self) {
  delete self;
}

// Interface: MyClass


MyClass* EMSCRIPTEN_KEEPALIVE emscripten_bind_MyClass_MyClass_0() {
  return new MyClass();
}

MyClass* EMSCRIPTEN_KEEPALIVE emscripten_bind_MyClass_MyClass_1(char* name) {
  return new MyClass(name);
}

char* EMSCRIPTEN_KEEPALIVE emscripten_bind_MyClass_get_name_0(MyClass* self) {
  return self->name;
}

void EMSCRIPTEN_KEEPALIVE emscripten_bind_MyClass_set_name_1(MyClass* self, char* arg0) {
  self->name = arg0;
}

void EMSCRIPTEN_KEEPALIVE emscripten_bind_MyClass___destroy___0(MyClass* self) {
  delete self;
}

// Interface: MyContainer


MyClass* EMSCRIPTEN_KEEPALIVE emscripten_bind_MyContainer_getItems_0(MyContainer* self) {
  static thread_local MyClass temp;
  return (temp = self->getItems(), &temp);
}

void EMSCRIPTEN_KEEPALIVE emscripten_bind_MyContainer_setItems_1(MyContainer* self, const MyClass* items) {
  self->setItems(*items);
}

void EMSCRIPTEN_KEEPALIVE emscripten_bind_MyContainer___destroy___0(MyContainer* self) {
  delete self;
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions