https://github.com/djdeveloperr/deno_objc
Objective-C runtime bridge for Deno
https://github.com/djdeveloperr/deno_objc
bridge deno ffi javascript objc objective-c runtime typescript
Last synced: 8 months ago
JSON representation
Objective-C runtime bridge for Deno
- Host: GitHub
- URL: https://github.com/djdeveloperr/deno_objc
- Owner: DjDeveloperr
- License: apache-2.0
- Created: 2022-01-29T09:24:14.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-06-09T18:12:17.000Z (over 2 years ago)
- Last Synced: 2025-03-31T01:51:18.341Z (9 months ago)
- Topics: bridge, deno, ffi, javascript, objc, objective-c, runtime, typescript
- Language: TypeScript
- Homepage: https://deno.land/x/objc
- Size: 1.43 MB
- Stars: 20
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Deno Objective-C
[](https://github.com/DjDeveloperr/deno_objc/releases)
[](https://doc.deno.land/https/deno.land/x/objc@0.1.0/mod.ts)
[](https://github.com/DjDeveloperr/deno_objc/actions/workflows/ci.yml)
[](https://github.com/DjDeveloperr/deno_objc/blob/master/LICENSE)
[](https://github.com/sponsors/DjDeveloperr)
Objective-C Runtime Bridge for Deno.
```ts
import objc from "https://deno.land/x/objc@0.1.0/mod.ts";
objc.import("AppKit");
const { NSPasteboard } = objc.classes;
const pasteboard = NSPasteboard.generalPasteboard();
const result = pasteboard.stringForType("public.utf8-plain-text");
// or
const result = objc
.send`${pasteboard} stringForType:${"public.utf8-plain-text"}`;
// Convert to JS String
console.log(result.UTF8String());
```
## Usage
This is mainly for interfacing with macOS Frameworks, but it can also be used
with GNUstep libobjc2.
By default, `libobjc.dylib`, `libobjc.so` or `objc.dll` will be loaded depending
on the platform.
If you want to override that, use `DENO_OBJC_PATH` env variable.
## API
To retrieve a class, use `objc.classes`:
```ts
const { NSPasteboard } = objc.classes;
```
To retrieve a protocol, use `objc.protocols`:
```ts
const { NSPasteboardReading } = objc.protocols;
```
In Obj-C, take the following method:
```cpp
- (void)setString:(NSString *)string;
```
Now to send it, you do
```cpp
[self setString:@"Hello World"];
```
But in JavaScript, you can do it in two ways. One is using the proxied method:
```js
self.setString_("Hello World");
// or
self.setString("Hello World");
```
Alternative way is to use `objc.send`:
```js
objc.send`${self} setString:${"Hello World"}`;
```
When sending a message, the types of course need to be converted into native
ones. For example, by default the native string type in Obj-C is actually just
null terminated C string. So the JS string will be converted to that if the
method needs.
Otherwise, if we find that the method takes an `id` type, we will try to convert
it to `NSString` instead.
Importing frameworks at runtime is done via `NSBundle`. By default, only
`Foundation` framework is loaded.
## Security
Since this module makes heavy use of FFI, it is inherently unsafe and gives
access to low level system primitives.
As such, to use this module you need to pass these flags:
- `--allow-env`: To check for a possible `DENO_OBJC_PATH` value
- `--allow-ffi`: To load ObjC runtime dynamic library
- `--unstable`: The FFI API in Deno is an unstable API (it can change)
The allow-ffi permission basically breaks through entire security sandbox, so
you can also just pass `-A`/`--allow-all`.
## License
[Apache-2.0](./LICENSE) licensed.
Copyright 2022 © DjDeveloperr