xref: /aosp_15_r20/external/skia/experimental/tskit/bindings/extension.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 #include <string>
8 
9 #include "experimental/tskit/bindings/bindings.h"
10 
11 // SkRect.h tossed in here to avoid having to include/link it in POC
12 typedef float SkScalar;
13 struct SkRect {
14     SkScalar fLeft;    //!< smaller x-axis bounds
15     SkScalar fTop;     //!< smaller y-axis bounds
16     SkScalar fRight;   //!< larger x-axis bounds
17     SkScalar fBottom;  //!< larger y-axis bounds
18 
containsSkRect19     bool contains(SkScalar x, SkScalar y) const {
20         return x >= fLeft && x < fRight && y >= fTop && y < fBottom;
21     }
22 };
23 
24 class Extension {
25 public:
Extension()26     Extension(): fProp("foo") {}
Extension(std::string n)27     Extension(std::string n): fProp(n) {}
28 
getProp()29     const std::string getProp() {
30         return fProp;
31     }
32 
setProp(std::string p)33     void setProp(std::string p) {
34         fProp = p;
35     }
36 
37 private:
38     std::string fProp;
39 };
40 
41 struct CompoundObj {
42     int alpha;
43     std::string beta;
44     float gamma;
45 };
46 
47 
EMSCRIPTEN_BINDINGS(Extension)48 EMSCRIPTEN_BINDINGS(Extension) {
49     TS_PRIVATE_EXPORT("_privateExtension(rPtr: number, len: number): number")
50     function("_privateExtension", optional_override([](uintptr_t rPtr, size_t len)->int {
51         int containsPoint = 0;
52         SkRect* rects = reinterpret_cast<SkRect*>(rPtr);
53         for (int i = 0; i < len; i++) {
54             if (rects[i].contains(5, 5)) {
55                 containsPoint++;
56             }
57         }
58         return containsPoint;
59     }));
60 
61     TS_PRIVATE_EXPORT("_withObject(obj: CompoundObj): void")
62     function("_withObject", optional_override([](CompoundObj o)->void {
63         printf("Object %d %s %f\n", o.alpha, o.beta.c_str(), o.gamma);
64     }));
65 
66     /**
67      * The Extension class extends the core components.
68      */
69     class_<Extension>("Extension")
70         .constructor<>()
71         /**
72          * Returns an extension with the provided property.
73          * @param name - if not provided, use a default value
74          */
75         TS_EXPORT("new(name?: string): Extension")
76         .constructor<std::string>()
77         /**
78          * Returns the associated property.
79          */
80         TS_EXPORT("getProp(): string")
81         .function("getProp", &Extension::getProp)
82         TS_PRIVATE_EXPORT("setProp(p: string): void")
83         .function("_setProp", &Extension::setProp);
84 
85     value_object<CompoundObj>("CompoundObj")
86         /** @type number */
87         .field("alpha", &CompoundObj::alpha)
88          /** @type string */
89         .field("beta", &CompoundObj::beta)
90         /**
91          * This field (gamma) should be documented.
92          * The default value is 1.0 if not set.
93          * @type @optional number
94          */
95         .field("gamma", &CompoundObj::gamma);
96 }
97