LinusU / LinusU/swift-napi-bindings

Need help with Data ValueConvertible

Open
#3 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
30
Forks
0
PR merge metrics
No merged PRs in 30d

Description

I'm new to swift, c, c++, and napi. So forgive the noobyness. I need to pass an object containing a Data (https://developer.apple.com/documentation/foundation/data) object from swift to nodejs (passing image data).

End goal would be Data on nodejs side as a byte array/buffer.

```
import NAPI
import NAPIC
import Foundation
import Cocoa

open class ScreenWithScreenshot : NAPI.ValueConvertible {

public let screenshotData : Data
public let screenIndex : Int
public let screenRect : NSRect
public let scale : CGFloat

public required init(_ screenIndex : Int, _ screen : NSScreen, _ screenshotData : Data) {
self.screenIndex = screenIndex
self.screenshotData = screenshotData
self.screenRect = CoordinateHelper.GetQuartzCoordinates(screen)
self.scale = screen.backingScaleFactor
}

public required convenience init(_ env: napi_env, from: napi_value) throws {
fatalError("Not implemented")
}

public func napiValue(_ env: napi_env) throws -> napi_value {

var result: napi_value!
var status: napi_status!

status = napi_create_object(env, &result)
guard status == napi_ok else { throw NAPI.Error(status) }

var screenIndex: napi_value!
status = napi_create_int32(env, Int32(self.screenIndex), &screenIndex)
guard status == napi_ok else { throw NAPI.Error(status) }

status = napi_set_named_property(env, result, "screenIndex", screenIndex)
guard status == napi_ok else { throw NAPI.Error(status) }

status = napi_set_named_property(env, result, "screenRect", try self.screenRect.napiValue(env))
guard status == napi_ok else { throw NAPI.Error(status) }

var scale: napi_value!
status = napi_create_double(env, Double(self.scale), &scale)
guard status == napi_ok else { throw NAPI.Error(status) }

status = napi_set_named_property(env, result, "scale", scale)
guard status == napi_ok else { throw NAPI.Error(status) }

// //struggling here
// var data: napi_value!
// status = napi_set_named_property(env, result, "data", self.screenshotData.bytes
// guard status == napi_ok else { throw NAPI.Error(status) }

return result
}
}
```

Extensions:
```
import NAPI
import NAPIC
import Foundation
import Cocoa

extension NSRect: NAPI.ValueConvertible {
public init(_ env: napi_env, from: napi_value) throws {
fatalError("Not implemented")
}

public func napiValue(_ env: napi_env) throws -> napi_value {
var status: napi_status!
var result: napi_value!

status = napi_create_object(env, &result)
guard status == napi_ok else { throw NAPI.Error(status) }

var x: napi_value!
status = napi_create_double(env, Double(self.minX), &x)
guard status == napi_ok else { throw NAPI.Error(status) }

var y: napi_value!
status = napi_create_double(env, Double(self.minY), &y)
guard status == napi_ok else { throw NAPI.Error(status) }

var width: napi_value!
status = napi_create_double(env, Double(self.width), &width)
guard status == napi_ok else { throw NAPI.Error(status) }

var height: napi_value!
status = napi_create_double(env, Double(self.height), &height)
guard status == napi_ok else { throw NAPI.Error(status) }

status = napi_set_named_property(env, result, "x", x)
guard status == napi_ok else { throw NAPI.Error(status) }

status = napi_set_named_property(env, result, "y", y)
guard status == napi_ok else { throw NAPI.Error(status) }

status = napi_set_named_property(env, result, "width", width)
guard status == napi_ok else { throw NAPI.Error(status) }

status = napi_set_named_property(env, result, "height", height)
guard status == napi_ok else { throw NAPI.Error(status) }

return result
}

}

extension Data {
var bytes : [UInt8] {
return [UInt8](self)
}
}
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the `ScreenWithScreenshot.napiValue(_:)` implementation and the commented `data` property code, then review the `Data` extension and NAPI value-conversion entry points shown in the issue. Done means the screenshot's Foundation `Data` is exposed on the Node.js side as a byte array or Buffer while the existing screen fields continue to convert correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, node.js, swift
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.