Skip to content

Commit

Permalink
feat: Implement a bunch of Swift types
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jul 19, 2024
1 parent 9af1465 commit 8673069
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/nitro-codegen/src/syntax/Method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Method implements CodeNode {
case 'swift': {
const params = this.parameters.map((p) => p.getCode('swift'))
const returnType = this.returnType.getCode('swift')
return `public func ${this.name}(${params.join(', ')}) throws ${returnType}`
return `public func ${this.name}(${params.join(', ')}) throws -> ${returnType}`
}
default:
throw new Error(
Expand Down
2 changes: 2 additions & 0 deletions packages/nitro-codegen/src/syntax/types/ArrayBufferType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class ArrayBufferType implements Type {
switch (language) {
case 'c++':
return 'std::shared_ptr<ArrayBuffer>'
case 'swift':
return 'Data'
default:
throw new Error(
`Language ${language} is not yet supported for ArrayBufferType!`
Expand Down
2 changes: 2 additions & 0 deletions packages/nitro-codegen/src/syntax/types/ArrayType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export class ArrayType implements Type {
switch (language) {
case 'c++':
return `std::vector<${itemCode}>`
case 'swift':
return `[${itemCode}]`
default:
throw new Error(
`Language ${language} is not yet supported for ArrayType!`
Expand Down
2 changes: 2 additions & 0 deletions packages/nitro-codegen/src/syntax/types/BigIntType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class BigIntType implements Type {
switch (language) {
case 'c++':
return 'int64_t'
case 'swift':
return 'Int64'
default:
throw new Error(
`Language ${language} is not yet supported for BigIntType!`
Expand Down
2 changes: 2 additions & 0 deletions packages/nitro-codegen/src/syntax/types/BooleanType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class BooleanType implements Type {
switch (language) {
case 'c++':
return 'bool'
case 'swift':
return 'Bool'
default:
throw new Error(
`Language ${language} is not yet supported for BooleanType!`
Expand Down
3 changes: 3 additions & 0 deletions packages/nitro-codegen/src/syntax/types/EnumType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class EnumType implements Type {
switch (language) {
case 'c++':
return this.enumName
case 'swift':
// TODO: Namespace?
return this.enumName
default:
throw new Error(
`Language ${language} is not yet supported for NumberType!`
Expand Down
2 changes: 2 additions & 0 deletions packages/nitro-codegen/src/syntax/types/NumberType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class NumberType implements Type {
switch (language) {
case 'c++':
return 'double'
case 'swift':
return 'Double'
default:
throw new Error(
`Language ${language} is not yet supported for NumberType!`
Expand Down
2 changes: 2 additions & 0 deletions packages/nitro-codegen/src/syntax/types/OptionalType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class OptionalType implements Type {
switch (language) {
case 'c++':
return `std::optional<${wrapping}>`
case 'swift':
return `${wrapping}?`
default:
throw new Error(
`Language ${language} is not yet supported for OptionalType!`
Expand Down
3 changes: 3 additions & 0 deletions packages/nitro-codegen/src/syntax/types/PromiseType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export class PromiseType implements Type {
switch (language) {
case 'c++':
return `std::future<${resultingCode}>`
case 'swift':
// TODO: Implement Promise in Swift!
return `Promise<${resultingCode}>`
default:
throw new Error(
`Language ${language} is not yet supported for PromiseType!`
Expand Down
2 changes: 2 additions & 0 deletions packages/nitro-codegen/src/syntax/types/StringType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class StringType implements Type {
switch (language) {
case 'c++':
return 'std::string'
case 'swift':
return 'String'
default:
throw new Error(
`Language ${language} is not yet supported for StringType!`
Expand Down
3 changes: 3 additions & 0 deletions packages/nitro-codegen/src/syntax/types/StructType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class StructType implements Type {
switch (language) {
case 'c++':
return this.structName
case 'swift':
// TODO: Namespace?
return this.structName
default:
throw new Error(
`Language ${language} is not yet supported for StructType!`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
///
/// HybridImage.cpp
/// Fri Jul 19 2024
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/react-native-nitro
/// Copyright © 2024 Marc Rousavy @ Margelo
///

#include "HybridImage.hpp"

void HybridImage::loadHybridMethods() {
// load base methods/properties
HybridObject::loadHybridMethods();
// load custom methods/properties
registerHybridGetter("width", &HybridImage::getWidth, this);
registerHybridGetter("height", &HybridImage::getHeight, this);
registerHybridGetter("pixelFormat", &HybridImage::getPixelFormat, this);
registerHybridMethod("toArrayBuffer", &HybridImage::toArrayBuffer, this);
registerHybridMethod("saveToFile", &HybridImage::saveToFile, this);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
///
/// HybridImage.hpp
/// Fri Jul 19 2024
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/react-native-nitro
/// Copyright © 2024 Marc Rousavy @ Margelo
///

#pragma once

#if __has_include(<NitroModules/HybridObject.hpp>)
#include <NitroModules/HybridObject.hpp>
#else
#error NitroModules cannot be found! Are you sure you installed react-native-nitro properly?
#endif

#include "PixelFormat.hpp"
#include "ImageFormat.hpp"

using namespace margelo::nitro;

/**
* An abstract base class for `Image`
* Inherit this class to create instances of `HybridImage` in C++.
* @example
* ```cpp
* class Image: public HybridImage {
* // ...
* };
* ```
*/
class HybridImage: public HybridObject {
public:
// Constructor
explicit HybridImage(): HybridObject(TAG) { }

public:
// Properties
virtual double getWidth() = 0;
virtual double getHeight() = 0;
virtual PixelFormat getPixelFormat() = 0;

public:
// Methods
virtual std::shared_ptr<ArrayBuffer> toArrayBuffer(ImageFormat format) = 0;
virtual std::future<void> saveToFile(const std::string& path) = 0;

protected:
// Tag for logging
static constexpr auto TAG = "Image";

private:
// Hybrid Setup
void loadHybridMethods() override;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
///
/// ImageFormat.hpp
/// Fri Jul 19 2024
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/react-native-nitro
/// Copyright © 2024 Marc Rousavy @ Margelo
///

#pragma once

#include <NitroModules/Hash.hpp>
#include <NitroModules/JSIConverter.hpp>

enum class ImageFormat {
jpg,
png,
} __attribute__((enum_extensibility(closed)));

namespace margelo::nitro {

// C++ ImageFormat <> JS ImageFormat (union)
template <>
struct JSIConverter<ImageFormat> {
static inline ImageFormat fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
switch (hashString(unionValue.c_str(), unionValue.size())) {
case hashString("jpg"): return ImageFormat::jpg;
case hashString("png"): return ImageFormat::png;
default: [[unlikely]]
throw std::runtime_error("Cannot convert " + unionValue + " to ImageFormat - invalid value!");
}
}
static inline jsi::Value toJSI(jsi::Runtime& runtime, ImageFormat arg) {
switch (arg) {
case ImageFormat::jpg: return JSIConverter<std::string>::toJSI(runtime, "jpg");
case ImageFormat::png: return JSIConverter<std::string>::toJSI(runtime, "png");
default: [[unlikely]]
throw std::runtime_error("Cannot convert ImageFormat to JS - invalid value: "
+ std::to_string(static_cast<int>(arg)) + "!");
}
}
};

} // namespace margelo::nitro
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
///
/// PixelFormat.hpp
/// Fri Jul 19 2024
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/react-native-nitro
/// Copyright © 2024 Marc Rousavy @ Margelo
///

#pragma once

#include <NitroModules/Hash.hpp>
#include <NitroModules/JSIConverter.hpp>

enum class PixelFormat {
rgb,
yuv_8bit,
yuv_10bit,
} __attribute__((enum_extensibility(closed)));

namespace margelo::nitro {

// C++ PixelFormat <> JS PixelFormat (union)
template <>
struct JSIConverter<PixelFormat> {
static inline PixelFormat fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
switch (hashString(unionValue.c_str(), unionValue.size())) {
case hashString("rgb"): return PixelFormat::rgb;
case hashString("yuv-8bit"): return PixelFormat::yuv_8bit;
case hashString("yuv-10bit"): return PixelFormat::yuv_10bit;
default: [[unlikely]]
throw std::runtime_error("Cannot convert " + unionValue + " to PixelFormat - invalid value!");
}
}
static inline jsi::Value toJSI(jsi::Runtime& runtime, PixelFormat arg) {
switch (arg) {
case PixelFormat::rgb: return JSIConverter<std::string>::toJSI(runtime, "rgb");
case PixelFormat::yuv_8bit: return JSIConverter<std::string>::toJSI(runtime, "yuv-8bit");
case PixelFormat::yuv_10bit: return JSIConverter<std::string>::toJSI(runtime, "yuv-10bit");
default: [[unlikely]]
throw std::runtime_error("Cannot convert PixelFormat to JS - invalid value: "
+ std::to_string(static_cast<int>(arg)) + "!");
}
}
};

} // namespace margelo::nitro
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
///
/// Image.swift
/// Fri Jul 19 2024
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/react-native-nitro
/// Copyright © 2024 Marc Rousavy @ Margelo
///

import Foundation

public protocol Image {
// Properties
public var width: Double { get }
public var height: Double { get }
public var pixelFormat: PixelFormat { get }

// Methods
public func toArrayBuffer(format: ImageFormat) throws -> Data
public func saveToFile(path: String) throws -> Promise<Void>
}

0 comments on commit 8673069

Please sign in to comment.