Example #1
0
jsi::Value TurboCxxModule::get(jsi::Runtime& runtime, const jsi::PropNameID& propName) {
  std::string propNameUtf8 = propName.utf8(runtime);

  if (propNameUtf8 == "getConstants") {
    // This is special cased because `getConstants()` is already a part of CxxModule.
    return jsi::Function::createFromHostFunction(
        runtime,
        propName,
        0,
        [this](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *args, size_t count) {
          jsi::Object result(rt);
          auto constants = cxxModule_->getConstants();
          for (auto &pair : constants) {
            result.setProperty(rt, pair.first.c_str(), jsi::valueFromDynamic(rt, pair.second));
          }
          return result;
        });
  }

  for (auto &method : cxxMethods_) {
    if (method.name == propNameUtf8) {
      return jsi::Function::createFromHostFunction(
          runtime,
          propName,
          0,
          [this, propNameUtf8](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *args, size_t count) {
            return invokeMethod(rt, VoidKind, propNameUtf8, args, count);
          });
    }
  }

  throw std::runtime_error("Function '" + propNameUtf8 + "' cannot be found on cxxmodule: " + name_);
}
jsi::Value TurboModule::get(jsi::Runtime& runtime, const jsi::PropNameID& propName) {
  std::string propNameUtf8 = propName.utf8(runtime);
  auto p = methodMap_.find(propNameUtf8);
  if (p == methodMap_.end()) {
    throw std::runtime_error("Function '" + propNameUtf8 + "' cannot be found on module: " + name_);
  }
  MethodMetadata meta = p->second;
  return jsi::Function::createFromHostFunction(
    runtime,
    propName,
    meta.argCount,
    [this, meta](facebook::jsi::Runtime &rt, const facebook::jsi::Value &thisVal, const facebook::jsi::Value *args, size_t count) {
      return meta.invoker(rt, *this, args, count);
    });
}
Example #3
0
jsi::Value TurboModule::get(jsi::Runtime& runtime, const jsi::PropNameID& propName) {
  std::string propNameUtf8 = propName.utf8(runtime);
  auto p = methodMap_.find(propNameUtf8);
  if (p == methodMap_.end()) {
    // Method was not found, let JS decide what to do.
    return jsi::Value::undefined();
  }
  MethodMetadata meta = p->second;
  return jsi::Function::createFromHostFunction(
    runtime,
    propName,
    meta.argCount,
    [this, meta](facebook::jsi::Runtime &rt, const facebook::jsi::Value &thisVal, const facebook::jsi::Value *args, size_t count) {
      return meta.invoker(rt, *this, args, count);
    });
}