Skip to content

Commit

Permalink
actually first Napoli commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gregcotten committed Nov 4, 2022
1 parent 3b2ab71 commit 5eb5f05
Show file tree
Hide file tree
Showing 35 changed files with 66 additions and 39 deletions.
26 changes: 26 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
MIT License

Copyright (c) 2022 Video Village LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

********************************************************************************

License from original fork: https://github.com/LinusU/swift-napi-bindings

MIT License

Copyright (c) 2019-2020 Linus Unnebäck

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import PackageDescription

let package = Package(
name: "swift-napi-bindings",
name: "Napoli",
platforms: [.macOS(.v10_15)],
products: [
.library(name: "NAPI", targets: ["NAPI"]),
.library(name: "Napoli", targets: ["Napoli"]),
.library(name: "NAPIC", targets: ["NAPIC"]),
],
targets: [
.target(name: "NAPI", dependencies: ["NAPIC"]),
.target(name: "Napoli", dependencies: ["NAPIC"]),
.target(name: "NAPIC", publicHeadersPath: "include"),
]
)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public protocol ErrorConvertible: Swift.Error {
}

internal func throwError(_ env: napi_env, _ error: Swift.Error) throws {
if let error = error as? NAPI.Error {
if let error = error as? Napoli.Error {
try error.napi_throw(env).throwIfError()
} else if let error = error as? ValueConvertible {
try napi_throw(env, error.napiValue(env)).throwIfError()
Expand All @@ -26,7 +26,6 @@ internal func exceptionIsPending(_ env: napi_env) throws -> Bool {
return result
}

public typealias AsyncCallback = (napi_env, Arguments) async throws -> ValueConvertible
public typealias Callback = (napi_env, Arguments) throws -> ValueConvertible

class CallbackData {
Expand Down Expand Up @@ -56,7 +55,7 @@ func swiftNAPICallback(_ env: napi_env!, _ cbinfo: napi_callback_info!) -> napi_

do {
return try data.callback(env, args as! Arguments).napiValue(env)
} catch NAPI.Error.pendingException {
} catch Napoli.Error.pendingException {
return nil
} catch {
if try! exceptionIsPending(env) == false { try! throwError(env, error) }
Expand All @@ -73,7 +72,7 @@ func swiftNAPIGetterCallback(_ env: napi_env!, _ cbinfo: napi_callback_info!) ->

do {
return try data.getter(env, args as! Arguments).napiValue(env)
} catch NAPI.Error.pendingException {
} catch Napoli.Error.pendingException {
return nil
} catch {
if try! exceptionIsPending(env) == false { try! throwError(env, error) }
Expand All @@ -90,7 +89,7 @@ func swiftNAPISetterCallback(_ env: napi_env!, _ cbinfo: napi_callback_info!) ->

do {
return try data.setter(env, args as! Arguments).napiValue(env)
} catch NAPI.Error.pendingException {
} catch Napoli.Error.pendingException {
return nil
} catch {
if try! exceptionIsPending(env) == false { try! throwError(env, error) }
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion Sources/NAPI/Error.swift → Sources/Napoli/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extension Error {
extension napi_status {
func throwIfError() throws {
guard self == napi_ok else {
throw NAPI.Error(self)
throw Napoli.Error(self)
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion Sources/NAPI/Null.swift → Sources/Napoli/Null.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public struct Null: ValueConvertible {
public init(_ env: napi_env, from: napi_value) throws {
guard try strictlyEquals(env, lhs: from, rhs: Null.default) else {
napi_throw_type_error(env, nil, "Expected null")
throw NAPI.Error.pendingException
throw Napoli.Error.pendingException
}
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func typedFuncNAPICallback(_ env: napi_env!, _ cbinfo: napi_callback_info!) -> n
}

return try data.callback(env, this, usedArgs).napiValue(env)
} catch NAPI.Error.pendingException {
} catch Napoli.Error.pendingException {
return nil
} catch {
if try! exceptionIsPending(env) == false { try! throwError(env, error) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public struct Undefined: ValueConvertible {
public init(_ env: napi_env, from: napi_value) throws {
guard try strictlyEquals(env, lhs: from, rhs: Undefined.default) else {
napi_throw_type_error(env, nil, "Expected undefined")
throw NAPI.Error.pendingException
throw Napoli.Error.pendingException
}
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions Tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
.DS_Store
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc

/.build/
/node_modules/
/package-lock.json
Expand Down

This file was deleted.

10 changes: 5 additions & 5 deletions Tests/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import PackageDescription

let package = Package(
name: "NAPITests",
name: "NapoliTests",
platforms: [.macOS(.v10_15)],
products: [
.library(name: "NAPITests", type: .dynamic, targets: ["NAPITests"]),
.library(name: "NapoliTests", type: .dynamic, targets: ["NapoliTests"]),
],
dependencies: [
.package(path: "../"),
],
targets: [
.target(name: "Trampoline",
dependencies: [.product(name: "NAPIC", package: "swift-napi-bindings")]),
.target(name: "NAPITests",
dependencies: [.product(name: "NAPIC", package: "Napoli")]),
.target(name: "NapoliTests",
dependencies: ["Trampoline",
.product(name: "NAPI", package: "swift-napi-bindings")]),
.product(name: "Napoli", package: "Napoli")]),
]
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import NAPI
import Napoli

struct TestError: Swift.Error, ErrorConvertible {
public var message: String
Expand Down Expand Up @@ -169,8 +169,8 @@ struct TestObject: ObjectConvertible {
}
}

@_cdecl("_init_napi_tests")
func initNAPITests(env: OpaquePointer, exports: OpaquePointer) -> OpaquePointer? {
@_cdecl("_init_napoli_tests")
func initNapoliTests(env: OpaquePointer, exports: OpaquePointer) -> OpaquePointer? {
initModule(env, exports, [
MethodDescriptor("returnString", returnString),
MethodDescriptor("returnDouble", returnDouble),
Expand Down
4 changes: 2 additions & 2 deletions Tests/Sources/Trampoline/trampoline.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <NAPI.h>

napi_value _init_napi_tests(napi_env, napi_value);
napi_value _init_napoli_tests(napi_env, napi_value);

NAPI_MODULE(napi_tests, _init_napi_tests)
NAPI_MODULE(napoli_tests, _init_napoli_tests)
2 changes: 1 addition & 1 deletion Tests/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./.build/release/NAPITests.node')
module.exports = require('./.build/release/NapoliTests.node')
2 changes: 1 addition & 1 deletion Tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"test": "standard && mocha",
"postinstall": "swift build -c release -Xlinker -undefined -Xlinker dynamic_lookup && mv .build/release/libNAPITests.dylib .build/release/NAPITests.node"
"postinstall": "swift build -c release -Xlinker -undefined -Xlinker dynamic_lookup && mv .build/release/libNapoliTests.dylib .build/release/NapoliTests.node"
},
"devDependencies": {
"mocha": "^6.0.0",
Expand Down
4 changes: 2 additions & 2 deletions bin/codegen
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SCRIPT_DIR="$(cd "$(dirname "$0")"; pwd)"

cd "$SCRIPT_DIR/../codegen/"
swift run codegen "${SCRIPT_DIR}/../Sources/NAPI/"
cd "${SCRIPT_DIR}/../Sources/NAPI/"
swift run codegen "${SCRIPT_DIR}/../Sources/Napoli/"
cd "${SCRIPT_DIR}/../Sources/Napoli/"
swiftformat *.generated.swift
2 changes: 1 addition & 1 deletion codegen/Sources/codegen/Generators/TypedFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ enum TypedFunction {
}
return try data.callback(env, this, usedArgs).napiValue(env)
} catch NAPI.Error.pendingException {
} catch Napoli.Error.pendingException {
return nil
} catch {
if try! exceptionIsPending(env) == false { try! throwError(env, error) }
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# N-API Swift Bindings
# Napoli

This package provides bindings to the [Node.js N-API](https://nodejs.org/api/n-api.html) so that you can build Node.js Addons in Swift.
This package provides bindings to the [Node.js N-API](https://nodejs.org/api/n-api.html) so that you can build Node.js Addons in Swift. Our intended usecase is for packaging Swift code in an Electron app for use on macOS, Linux, and Windows(!!!), but this can work in other Node.js environments as well.

**Note:** This is very much a work in progress, expect the API to change drastically between versions! I'll be adding documentation for how to get up and running, and building addons soon™
This project was originally a fork of [this LinusU repo](https://github.com/LinusU/swift-napi-bindings) but has changed significantally enough to warrant the creation of its own repo.

See some simple examples here: [LinusU/swift-node-addon-examples](https://github.com/LinusU/swift-node-addon-examples)
The next step is to create a sample project to show how to actually use Napoli with Electron.

0 comments on commit 5eb5f05

Please sign in to comment.