// // Copyright Contributors to the MaterialX Project // SPDX-License-Identifier: Apache-2.0 // #include #include #include #include #include #include namespace ems = emscripten; namespace mx = MaterialX; #define BIND_ITERABLE_PROTOCOL(NAME) \ .function("next", ems::optional_override([](mx::NAME &it) { \ bool done = ++it == it.end(); \ ems::val result = ems::val::object(); \ result.set("done", done); \ if (!done) result.set("value", *it); \ return result; \ })); \ EM_ASM( \ Module[#NAME]['prototype'][Symbol.iterator] = function() { return this; }; \ ); EMSCRIPTEN_BINDINGS(traversal) { ems::class_("Edge") .smart_ptr_constructor("Edge", &std::make_shared) .function("equals", ems::optional_override([](mx::Edge &self, const mx::Edge &rhs) { return self == rhs; })) .function("notEquals", ems::optional_override([](mx::Edge &self, const mx::Edge &rhs) { return self != rhs; })) .function("lessThan", ems::optional_override([](mx::Edge &self, const mx::Edge &rhs) { return self < rhs; })) .function("notNull", &mx::Edge::operator bool) .function("getDownstreamElement", &mx::Edge::getDownstreamElement) .function("getConnectingElement", &mx::Edge::getConnectingElement) .function("getUpstreamElement", &mx::Edge::getUpstreamElement) .function("getName", &mx::Edge::getName); ems::class_("TreeIterator") .smart_ptr_constructor("TreeIterator", &std::make_shared) .function("getElement", &mx::TreeIterator::getElement) .function("getElementDepth", &mx::TreeIterator::getElementDepth) .function("setPruneSubtree", &mx::TreeIterator::setPruneSubtree) .function("getPruneSubtree", &mx::TreeIterator::getPruneSubtree) BIND_ITERABLE_PROTOCOL(TreeIterator) ems::class_("GraphIterator") .smart_ptr_constructor("GraphIterator", &std::make_shared) .function("getDownstreamElement", &mx::GraphIterator::getDownstreamElement) .function("getConnectingElement", &mx::GraphIterator::getConnectingElement) .function("getUpstreamElement", &mx::GraphIterator::getUpstreamElement) .function("getUpstreamIndex", &mx::GraphIterator::getUpstreamIndex) .function("getElementDepth", &mx::GraphIterator::getElementDepth) .function("getNodeDepth", &mx::GraphIterator::getNodeDepth) .function("setPruneSubgraph", &mx::GraphIterator::setPruneSubgraph) .function("getPruneSubgraph", &mx::GraphIterator::getPruneSubgraph) BIND_ITERABLE_PROTOCOL(GraphIterator) ems::class_("InheritanceIterator") .smart_ptr_constructor("InheritanceIterator", &std::make_shared) BIND_ITERABLE_PROTOCOL(InheritanceIterator) ems::function("getNullEdge", &mx::getNullEdge); }