Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/react-native-codegen/src/CodegenSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ export interface NativeModuleMixedTypeAnnotation {
readonly type: 'MixedTypeAnnotation';
}

export interface NativeModuleArrayBufferTypeAnnotation {
readonly type: 'ArrayBufferTypeAnnotation';
}

export type NativeModuleEventEmitterBaseTypeAnnotation =
| NativeModuleBooleanTypeAnnotation
| NativeModuleDoubleTypeAnnotation
Expand Down Expand Up @@ -434,6 +438,7 @@ export type NativeModuleBaseTypeAnnotation =
| NativeModuleObjectTypeAnnotation
| NativeModuleUnionTypeAnnotation
| NativeModuleMixedTypeAnnotation
| NativeModuleArrayBufferTypeAnnotation
| NativeModuleArrayTypeAnnotation<NativeModuleBaseTypeAnnotation>;

export type NativeModuleParamTypeAnnotation =
Expand Down
7 changes: 6 additions & 1 deletion packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ export type NativeModuleMixedTypeAnnotation = Readonly<{
type: 'MixedTypeAnnotation',
}>;

export type NativeModuleArrayBufferTypeAnnotation = Readonly<{
type: 'ArrayBufferTypeAnnotation',
}>;

type NativeModuleEventEmitterBaseTypeAnnotation =
| BooleanTypeAnnotation
| DoubleTypeAnnotation
Expand Down Expand Up @@ -428,7 +432,8 @@ export type NativeModuleBaseTypeAnnotation =
| NativeModuleArrayTypeAnnotation<Nullable<NativeModuleBaseTypeAnnotation>>
| NativeModuleObjectTypeAnnotation
| NativeModuleUnionTypeAnnotation
| NativeModuleMixedTypeAnnotation;
| NativeModuleMixedTypeAnnotation
| NativeModuleArrayBufferTypeAnnotation;

export type NativeModuleParamTypeAnnotation =
| NativeModuleBaseTypeAnnotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ function serializeArg(
return wrap(val => `${val}.asObject(rt)`);
case 'MixedTypeAnnotation':
return wrap(val => `jsi::Value(rt, ${val})`);
case 'ArrayBufferTypeAnnotation':
return wrap(val => `${val}.asObject(rt).getArrayBuffer(rt)`);
default:
realTypeAnnotation.type as empty;
throw new Error(
Expand Down Expand Up @@ -307,6 +309,8 @@ function translatePrimitiveJSTypeToCpp(
return wrapOptional('jsi::Value', isRequired);
case 'MixedTypeAnnotation':
return wrapOptional('jsi::Value', isRequired);
case 'ArrayBufferTypeAnnotation':
return wrapOptional('jsi::ArrayBuffer', isRequired);
default:
realTypeAnnotation.type as empty;
throw new Error(createErrorMessage(realTypeAnnotation.type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ function translateEventEmitterTypeToJavaType(
case 'FloatTypeAnnotation':
case 'Int32TypeAnnotation':
case 'VoidTypeAnnotation':
case 'ArrayBufferTypeAnnotation':
// TODO: Add support for these types
throw new Error(
`Unsupported eventType for ${eventEmitter.name}. Found: ${eventEmitter.typeAnnotation.typeAnnotation.type}`,
Expand Down Expand Up @@ -267,6 +268,9 @@ function translateFunctionParamToJavaType(
case 'FunctionTypeAnnotation':
imports.add('com.facebook.react.bridge.Callback');
return wrapOptional('Callback', isRequired);
case 'ArrayBufferTypeAnnotation':
imports.add('java.nio.ByteBuffer');
return wrapOptional('ByteBuffer', isRequired);
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(createErrorMessage(realTypeAnnotation.type));
Expand Down Expand Up @@ -361,6 +365,9 @@ function translateFunctionReturnTypeToJavaType(
case 'ArrayTypeAnnotation':
imports.add('com.facebook.react.bridge.WritableArray');
return wrapOptional('WritableArray', isRequired);
case 'ArrayBufferTypeAnnotation':
imports.add('java.nio.ByteBuffer');
return wrapOptional('ByteBuffer', isRequired);
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(createErrorMessage(realTypeAnnotation.type));
Expand Down Expand Up @@ -443,6 +450,8 @@ function getFalsyReturnStatementFromReturnType(
return 'return null;';
case 'ArrayTypeAnnotation':
return 'return null;';
case 'ArrayBufferTypeAnnotation':
return 'return null;';
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(createErrorMessage(realTypeAnnotation.type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type JSReturnType =
| 'NumberKind'
| 'PromiseKind'
| 'ObjectKind'
| 'ArrayKind';
| 'ArrayKind'
| 'ArrayBufferKind';

const HostFunctionTemplate = ({
hasteModuleName,
Expand Down Expand Up @@ -216,6 +217,8 @@ function translateReturnTypeToKind(
return 'ObjectKind';
case 'ArrayTypeAnnotation':
return 'ArrayKind';
case 'ArrayBufferTypeAnnotation':
return 'ArrayBufferKind';
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(
Expand Down Expand Up @@ -303,6 +306,8 @@ function translateParamTypeToJniType(
return 'Lcom/facebook/react/bridge/ReadableArray;';
case 'FunctionTypeAnnotation':
return 'Lcom/facebook/react/bridge/Callback;';
case 'ArrayBufferTypeAnnotation':
return 'Ljava/nio/ByteBuffer;';
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(
Expand Down Expand Up @@ -387,6 +392,8 @@ function translateReturnTypeToJniType(
return 'Lcom/facebook/react/bridge/WritableMap;';
case 'ArrayTypeAnnotation':
return 'Lcom/facebook/react/bridge/WritableArray;';
case 'ArrayBufferTypeAnnotation':
return 'Ljava/nio/ByteBuffer;';
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class StructCollector {
return wrapNullable(nullable, typeAnnotation);
case 'MixedTypeAnnotation':
throw new Error('Mixed types are unsupported in structs');
case 'ArrayBufferTypeAnnotation':
throw new Error('ArrayBuffer types are unsupported in structs');
case 'UnionTypeAnnotation':
try {
const validUnionType = parseValidUnionType(typeAnnotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ type ReturnJSType =
| 'ObjectKind'
| 'ArrayKind'
| 'NumberKind'
| 'StringKind';
| 'StringKind'
| 'ArrayBufferKind';

export type MethodSerializationOutput = Readonly<{
methodName: string,
Expand Down Expand Up @@ -219,6 +220,9 @@ function getParamObjCType(
*/
return notStruct(wrapOptional('NSArray *', !nullable));
}
case 'ArrayBufferTypeAnnotation': {
return notStruct(wrapOptional('NSData *', !nullable));
}
}

const [structTypeAnnotation] = unwrapNullable(
Expand Down Expand Up @@ -387,6 +391,8 @@ function getReturnObjCType(
}
case 'GenericObjectTypeAnnotation':
return wrapOptional('NSDictionary *', isRequired);
case 'ArrayBufferTypeAnnotation':
return wrapOptional('NSData *', isRequired);
default:
typeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(
Expand Down Expand Up @@ -433,6 +439,8 @@ function getReturnJSType(
return 'BooleanKind';
case 'GenericObjectTypeAnnotation':
return 'ObjectKind';
case 'ArrayBufferTypeAnnotation':
return 'ArrayBufferKind';
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,74 @@ const STRING_LITERALS: SchemaType = {
},
};

const ARRAYBUFFER_MODULE: SchemaType = {
modules: {
NativeSampleTurboModule: {
type: 'NativeModule',
aliasMap: {},
enumMap: {},
spec: {
eventEmitters: [],
methods: [
{
name: 'getArrayBuffer',
optional: false,
typeAnnotation: {
type: 'FunctionTypeAnnotation',
returnTypeAnnotation: {
type: 'ArrayBufferTypeAnnotation',
},
params: [
{
name: 'buffer',
optional: false,
typeAnnotation: {
type: 'ArrayBufferTypeAnnotation',
},
},
],
},
},
{
name: 'saveData',
optional: false,
typeAnnotation: {
type: 'FunctionTypeAnnotation',
returnTypeAnnotation: {
type: 'VoidTypeAnnotation',
},
params: [
{
name: 'data',
optional: false,
typeAnnotation: {
type: 'ArrayBufferTypeAnnotation',
},
},
],
},
},
{
name: 'loadData',
optional: false,
typeAnnotation: {
type: 'FunctionTypeAnnotation',
returnTypeAnnotation: {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'ArrayBufferTypeAnnotation',
},
},
params: [],
},
},
],
},
moduleName: 'SampleTurboModule',
},
},
};

module.exports = {
complex_objects: COMPLEX_OBJECTS,
two_modules_different_files: TWO_MODULES_DIFFERENT_FILES,
Expand All @@ -2804,4 +2872,5 @@ module.exports = {
SampleWithUppercaseName: SAMPLE_WITH_UPPERCASE_NAME,
union_module: UNION_MODULE,
string_literals: STRING_LITERALS,
arraybuffer_module: ARRAYBUFFER_MODULE,
};
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,67 @@ private:
}
`;

exports[`GenerateModuleH can generate fixture arraybuffer_module 1`] = `
Map {
"arraybuffer_moduleJSI.h" => "/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* @generated by codegen project: GenerateModuleH.js
*/

#pragma once

#include <ReactCommon/TurboModule.h>
#include <react/bridging/Bridging.h>

namespace facebook::react {


template <typename T>
class JSI_EXPORT NativeSampleTurboModuleCxxSpec : public TurboModule {
public:
static constexpr std::string_view kModuleName = \\"SampleTurboModule\\";

protected:
NativeSampleTurboModuleCxxSpec(std::shared_ptr<CallInvoker> jsInvoker) : TurboModule(std::string{NativeSampleTurboModuleCxxSpec::kModuleName}, jsInvoker) {
methodMap_[\\"getArrayBuffer\\"] = MethodMetadata {.argCount = 1, .invoker = __getArrayBuffer};
methodMap_[\\"saveData\\"] = MethodMetadata {.argCount = 1, .invoker = __saveData};
methodMap_[\\"loadData\\"] = MethodMetadata {.argCount = 0, .invoker = __loadData};
}

private:
static jsi::Value __getArrayBuffer(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
static_assert(
bridging::getParameterCount(&T::getArrayBuffer) == 2,
\\"Expected getArrayBuffer(...) to have 2 parameters\\");
return bridging::callFromJs<jsi::ArrayBuffer>(rt, &T::getArrayBuffer, static_cast<NativeSampleTurboModuleCxxSpec*>(&turboModule)->jsInvoker_, static_cast<T*>(&turboModule),
count <= 0 ? throw jsi::JSError(rt, \\"Expected argument in position 0 to be passed\\") : args[0].asObject(rt).getArrayBuffer(rt));
}

static jsi::Value __saveData(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
static_assert(
bridging::getParameterCount(&T::saveData) == 2,
\\"Expected saveData(...) to have 2 parameters\\");
bridging::callFromJs<void>(rt, &T::saveData, static_cast<NativeSampleTurboModuleCxxSpec*>(&turboModule)->jsInvoker_, static_cast<T*>(&turboModule),
count <= 0 ? throw jsi::JSError(rt, \\"Expected argument in position 0 to be passed\\") : args[0].asObject(rt).getArrayBuffer(rt));return jsi::Value::undefined();
}

static jsi::Value __loadData(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* /*args*/, size_t /*count*/) {
static_assert(
bridging::getParameterCount(&T::loadData) == 1,
\\"Expected loadData(...) to have 1 parameters\\");
auto result = bridging::callFromJs<std::optional<jsi::ArrayBuffer>>(rt, &T::loadData, static_cast<NativeSampleTurboModuleCxxSpec*>(&turboModule)->jsInvoker_, static_cast<T*>(&turboModule));return result ? jsi::Value(std::move(*result)) : jsi::Value::null();
}
};

} // namespace facebook::react
",
}
`;

exports[`GenerateModuleH can generate fixture complex_objects 1`] = `
Map {
"complex_objectsJSI.h" => "/**
Expand Down
Loading
Loading