skia2/experimental/tskit/bindings/core.cpp
Kevin Lubick ed0c0ed028 [canvaskit] Add initial implementation of generating TS
This adds some initial features for generating the ambient
namespace files (the TS equivalent of .h files) for the
C++ binding files. There are a few tests and it works on
the sample code in tskit.

I split the bindings/ folder into bindings/ and interface/
(for C++ and TS code, respectively).

This can be tested locally by running
make generate && make debug && make serve
and verifying that everything compiles and runs in the browser.

One thing I changed for easier parsing was making a @type
annotation required, with an @optional tag for making a field
or constant optional.

Change-Id: If9c5a664e84fac4c734277cddd7774989a1f7bf8
Bug: skia:11826
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/407476
Reviewed-by: Nathaniel Nifong <nifong@google.com>
Reviewed-by: Leandro Lovisolo <lovisolo@google.com>
2021-05-17 13:56:31 +00:00

60 lines
1.5 KiB
C++

/*
* Copyright 2021 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <string>
#include "experimental/tskit/bindings/bindings.h"
class Something {
public:
Something(std::string n): fName(n) {}
const std::string getName() {
return fName;
}
void setName(std::string name) {
fName = name;
}
private:
std::string fName;
};
EMSCRIPTEN_BINDINGS(Core) {
TS_PRIVATE_EXPORT("_privateFunction(x: number, y: number): number")
function("_privateFunction", optional_override([](int x, int y)->size_t {
return x * y;
}));
/**
* This function does a public thing.
* @param input an ice cream flavor
*/
TS_EXPORT("publicFunction(input: string): void")
function("publicFunction", optional_override([](std::string s)->void {
printf("Hello %s\n", s.c_str());
}));
/**
* The Something class is quite something. See SkSomething.h for more.
*/
class_<Something>("Something")
/**
* Returns a Something with the provided name.
* @param name
*/
TS_EXPORT("new(name: string): Something")
.constructor<std::string>()
/**
* Returns the associated name.
*/
TS_EXPORT("getName(): string")
.function("getName", &Something::getName)
TS_PRIVATE_EXPORT("setName(name: string): void")
.function("_setName", &Something::setName);
}