diff --git a/packages/react-native/ReactCommon/jsi/jsi/decorator.h b/packages/react-native/ReactCommon/jsi/jsi/decorator.h index 5e57b44a87dfeb..2907ea6bb1df37 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/decorator.h +++ b/packages/react-native/ReactCommon/jsi/jsi/decorator.h @@ -232,6 +232,10 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation { return plain_.utf16(sym); } + Object createObjectWithPrototype(const Object& prototype) override { + return plain_.createObjectWithPrototype(prototype); + } + Object createObject() override { return plain_.createObject(); }; @@ -695,6 +699,11 @@ class WithRuntimeDecorator : public RuntimeDecorator { return RD::createValueFromJsonUtf8(json, length); }; + Object createObjectWithPrototype(const Object& prototype) override { + Around around{with_}; + return RD::createObjectWithPrototype(prototype); + } + Object createObject() override { Around around{with_}; return RD::createObject(); diff --git a/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp b/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp index e11ef30d77ed65..d907f36c111c54 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp +++ b/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp @@ -258,6 +258,13 @@ std::u16string Runtime::utf16(const String& str) { return convertUTF8ToUTF16(utf8Str); } +Object Runtime::createObjectWithPrototype(const Object& prototype) { + auto createFn = global() + .getPropertyAsObject(*this, "Object") + .getPropertyAsFunction(*this, "create"); + return createFn.call(*this, prototype).asObject(*this); +} + Pointer& Pointer::operator=(Pointer&& other) noexcept { if (ptr_) { ptr_->invalidate(); diff --git a/packages/react-native/ReactCommon/jsi/jsi/jsi.h b/packages/react-native/ReactCommon/jsi/jsi/jsi.h index be48bb82474352..039af8006303e5 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/jsi.h +++ b/packages/react-native/ReactCommon/jsi/jsi/jsi.h @@ -332,6 +332,7 @@ class JSI_EXPORT Runtime { virtual Object createObject(std::shared_ptr ho) = 0; virtual std::shared_ptr getHostObject(const jsi::Object&) = 0; virtual HostFunctionType& getHostFunction(const jsi::Function&) = 0; + virtual Object createObjectWithPrototype(const Object& prototype); virtual bool hasNativeState(const jsi::Object&) = 0; virtual std::shared_ptr getNativeState(const jsi::Object&) = 0; @@ -688,6 +689,11 @@ class JSI_EXPORT Object : public Pointer { return runtime.createObject(ho); } + /// Creates a new Object with the custom prototype + Object create(Runtime& runtime, const Object& prototype) { + return runtime.createObjectWithPrototype(prototype); + } + /// \return whether this and \c obj are the same JSObject or not. static bool strictEquals(Runtime& runtime, const Object& a, const Object& b) { return runtime.strictEquals(a, b); diff --git a/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp b/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp index 57d5afbcfc41c3..2fde372ea88f8f 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp +++ b/packages/react-native/ReactCommon/jsi/jsi/test/testlib.cpp @@ -1635,6 +1635,27 @@ TEST_P(JSITest, UTF16Test) { EXPECT_EQ(str.utf16(rd), u"\uFFFD\u007A"); } +TEST_P(JSITest, ObjectCreateWithPrototype) { + // This Runtime Decorator is used to test the default implementation of + // Object.create(prototype) + class RD : public RuntimeDecorator { + public: + RD(Runtime& rt) : RuntimeDecorator(rt) {} + + Object createObjectWithPrototype(const Object& prototype) override { + return Runtime::createObjectWithPrototype(prototype); + } + }; + + RD rd = RD(rt); + Object prototype(rd); + + prototype.setProperty(rd, "someProperty", 123); + + Object child = rd.createObjectWithPrototype(prototype); + EXPECT_EQ(child.getProperty(rd, "someProperty").getNumber(), 123); +} + INSTANTIATE_TEST_CASE_P( Runtimes, JSITest,