Flutter Engine
The Flutter Engine
extension.cpp
Go to the documentation of this file.
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
10
11// SkRect.h tossed in here to avoid having to include/link it in POC
12typedef float SkScalar;
13struct 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
19 bool contains(SkScalar x, SkScalar y) const {
20 return x >= fLeft && x < fRight && y >= fTop && y < fBottom;
21 }
22};
23
24class Extension {
25public:
26 Extension(): fProp("foo") {}
27 Extension(std::string n): fProp(n) {}
28
29 const std::string getProp() {
30 return fProp;
31 }
32
33 void setProp(std::string p) {
34 fProp = p;
35 }
36
37private:
38 std::string fProp;
39};
40
42 int alpha;
43 std::string beta;
44 float gamma;
45};
46
47
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}
#define TS_PRIVATE_EXPORT(ts_code)
Definition: bindings.h:44
#define TS_EXPORT(ts_code)
Definition: bindings.h:45
const std::string getProp()
Definition: extension.cpp:29
Extension(std::string n)
Definition: extension.cpp:27
void setProp(std::string p)
Definition: extension.cpp:33
EMSCRIPTEN_BINDINGS(Extension)
Definition: extension.cpp:48
float SkScalar
Definition: extension.cpp:12
Dart_NativeFunction function
Definition: fuchsia.cc:51
std::string printf(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: SkSLString.cpp:83
constexpr bool contains(std::string_view str, std::string_view needle)
Definition: SkStringView.h:41
float gamma
Definition: extension.cpp:44
std::string beta
Definition: extension.cpp:43
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition: extension.cpp:14
constexpr float x() const
Definition: SkRect.h:720
constexpr float y() const
Definition: SkRect.h:727
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
bool contains(SkScalar x, SkScalar y) const
Definition: extension.cpp:19
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15