@tokamakdev/platform-android-arm64 0.1.1-beta.127 → 0.1.1-beta.128

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -156,11 +156,6 @@ plugins {
156
156
  android {
157
157
  namespace 'com.tokamak.runtime.plugin'
158
158
  }
159
-
160
- // Plugins mark code that needs a later API level with RequiresApi, which lint checks.
161
- dependencies {
162
- api 'androidx.annotation:annotation:1.9.1'
163
- }
164
159
  EOF
165
160
 
166
161
  has_icon=false
@@ -187,21 +182,13 @@ raw_names=" "
187
182
  for plugin in "$input/plugins"/*/; do
188
183
  plugin_id=$(basename "$plugin")
189
184
  class=$(cat "$plugin/class" 2>/dev/null || true)
190
- # A plugin whose native code is its generated bindings alone has no class.
191
- if [[ -n "$class" || ! -d "$plugin/bindings" ]]; then
192
- if [[ ! "$class" =~ $kotlin_class ]]; then
193
- echo "plugin '$plugin_id' has invalid android class '$class'" >&2
194
- exit 1
195
- fi
196
- printf ' %s(app.host("%s")),\n' "$class" "$plugin_id" >> "$registry"
185
+ if [[ ! "$class" =~ $kotlin_class ]]; then
186
+ echo "plugin '$plugin_id' has invalid android class '$class'" >&2
187
+ exit 1
197
188
  fi
189
+ printf ' %s(app.host("%s")),\n' "$class" "$plugin_id" >> "$registry"
198
190
  module=$plugin_modules/$plugin_id
199
191
  mkdir -p "$module/src/main/kotlin"
200
- # `tok bindings` names the generated class com.tokamak.bindings.<id>.Bindings, with - as _.
201
- for source in "$plugin/bindings"/*.kt; do
202
- printf ' com.tokamak.bindings.%s.Bindings(app.host("%s")),\n' "${plugin_id//-/_}" "$plugin_id" >> "$registry"
203
- cp "$source" "$module/src/main/kotlin/TokamakBindings.kt"
204
- done
205
192
  for source in "$plugin/sources"/*; do
206
193
  case "$source" in
207
194
  *.kt)
@@ -11,19 +11,14 @@ internal class TokamakCorePlugin(private val app: TokamakApplication) : TokamakP
11
11
  override val id = "tokamak"
12
12
  private val executor = Executors.newSingleThreadExecutor()
13
13
 
14
- /** WebView operations and the activity run on the main thread, which WebView and activities require. */
14
+ /** WebView operations run on the main thread, which WebView requires. */
15
15
  override fun executor(method: String): Executor =
16
- if (method.startsWith("webview.") || method == "resource.root") super.executor(method) else executor
16
+ if (method.startsWith("webview.")) super.executor(method) else executor
17
17
 
18
18
  override fun call(method: String, arguments: Any?, reply: TokamakPluginReply): (() -> Unit)? {
19
19
  val view = (app.activity as? TokamakActivity)?.webView
20
20
  when (method) {
21
21
  "resource.read" -> reply(Result.success(app.resources.get(arguments, ByteArray::class.java)))
22
- "resource.release" -> {
23
- app.resources.release(arguments)
24
- reply(Result.success(null))
25
- }
26
- "resource.root" -> reply(Result.success(root(arguments as JSONObject)))
27
22
  "webview.getCurrentPage" -> reply(Result.success(view?.url))
28
23
  "webview.navigate" -> {
29
24
  if (view == null) throw noView()
@@ -49,24 +44,6 @@ internal class TokamakCorePlugin(private val app: TokamakApplication) : TokamakP
49
44
  return null
50
45
  }
51
46
 
52
- /** `resource.scope` opens a scope for the objects an invocation receives, which ends with the subscription. */
53
- override fun subscribe(method: String, arguments: Any?, reply: TokamakPluginReply): () -> Unit {
54
- if (method != "resource.scope") throw TokamakPluginError.notSupported("$id.$method is not supported")
55
- val scope = app.resources.open()
56
- reply(Result.success(scope))
57
- return { app.resources.end(scope) }
58
- }
59
-
60
- /** Retains the app object [arguments] names in its scope and returns its ID: the application `context` or the foreground `activity`. */
61
- private fun root(arguments: JSONObject): String {
62
- val root: Any = when (arguments.getString("name")) {
63
- "context" -> app
64
- "activity" -> app.requireForegroundActivity()
65
- else -> throw TokamakPluginError.notSupported("$id.resource.root has no such object")
66
- }
67
- return app.resources.retain(root, arguments.getString("scope"))
68
- }
69
-
70
47
  private fun noView() = TokamakPluginError("InvalidStateError", "The app has no WebView")
71
48
 
72
49
  private fun notAppUrl() = TokamakPluginError.typeError("Navigation requires an app URL")
@@ -2,50 +2,21 @@ package com.tokamak.runtime
2
2
 
3
3
  import java.util.UUID
4
4
 
5
- /** Native objects that plugins share by ID. Each belongs to a scope and is kept until it is released or its scope ends. */
5
+ /** Native objects that plugins share by ID. Each is kept until the subscription that retained it ends. */
6
6
  class TokamakResources {
7
- private val objects = HashMap<String, Pair<Any, String>>()
8
- private val scopes = HashMap<String, MutableSet<String>>()
7
+ private val objects = HashMap<String, Any>()
9
8
 
10
- /** Starts a scope and returns its ID. */
11
- fun open(): String {
12
- val scope = UUID.randomUUID().toString()
13
- synchronized(this) { scopes[scope] = HashSet() }
14
- return scope
15
- }
16
-
17
- /** Keeps [value] in [scope] and returns its ID. An ended scope keeps nothing, so the ID finds no object. */
18
- fun retain(value: Any, scope: String): String {
19
- val id = UUID.randomUUID().toString()
20
- synchronized(this) {
21
- if (scopes[scope]?.add(id) == true) objects[id] = value to scope
22
- }
23
- return id
24
- }
25
-
26
- /** Keeps [value] in a scope of its own, replies with its ID, and returns the release to return from `subscribe`. */
9
+ /** Keeps [value], replies with its ID, and returns the release to return from `subscribe`. */
27
10
  fun retain(value: Any, reply: TokamakPluginReply): () -> Unit {
28
- val scope = open()
29
- reply(Result.success(retain(value, scope)))
30
- return { end(scope) }
31
- }
32
-
33
- /** Releases the object retained as [id], if it is still kept. */
34
- fun release(id: Any?) {
35
- synchronized(this) {
36
- val (_, scope) = objects.remove(id as? String) ?: return
37
- scopes[scope]?.remove(id)
38
- }
39
- }
40
-
41
- /** Releases every object [scope] keeps, and keeps nothing in it later. */
42
- fun end(scope: String) {
43
- synchronized(this) { scopes.remove(scope)?.forEach { objects.remove(it) } }
11
+ val id = UUID.randomUUID().toString()
12
+ synchronized(this) { objects[id] = value }
13
+ reply(Result.success(id))
14
+ return { synchronized(this) { objects.remove(id) } }
44
15
  }
45
16
 
46
17
  /** The object retained as [id]. */
47
18
  fun <T : Any> get(id: Any?, type: Class<T>): T {
48
- val value = synchronized(this) { objects[id as? String]?.first }
19
+ val value = synchronized(this) { objects[id as? String] }
49
20
  ?: throw TokamakPluginError("InvalidStateError", "The native reference is closed")
50
21
  if (!type.isInstance(value)) throw TokamakPluginError.typeError("The native reference is not ${type.simpleName}")
51
22
  return type.cast(value)!!
@@ -24,7 +24,6 @@
24
24
  }
25
25
  },
26
26
  "pluginKeys": {
27
- "bindings": "paths",
28
27
  "class": "string",
29
28
  "dependencies": "strings",
30
29
  "manifest": "path",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokamakdev/platform-android-arm64",
3
- "version": "0.1.1-beta.127",
3
+ "version": "0.1.1-beta.128",
4
4
  "description": "The tokamak platform pack for android-arm64",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,172 +0,0 @@
1
- package com.tokamak.runtime
2
-
3
- import java.util.Base64
4
- import org.json.JSONArray
5
- import org.json.JSONObject
6
-
7
- /**
8
- * One call or subscription of a generated platform binding. It decodes the Worker's JSON into
9
- * native values and encodes native values back, keeping the native objects it returns in the
10
- * receiving invocation's scope.
11
- *
12
- * JSON objects tagged `#` carry what JSON lacks: native objects, bytes, errors, non-finite
13
- * numbers, and records whose keys include `#`.
14
- */
15
- class TokamakBinding(payload: ByteArray, private val resources: TokamakResources, private val reply: TokamakPluginBytesReply) {
16
- companion object {
17
- /** The rejection of a member the running API level lacks. */
18
- val unavailable get() = TokamakPluginError.notSupported("The API is unavailable on this API level")
19
- }
20
-
21
- private val request = decodePluginValue(payload) as? JSONObject
22
- ?: throw TokamakPluginError.typeError("A binding request is an object")
23
-
24
- /** Argument [index] as JSON. */
25
- operator fun get(index: Int): Any? = request.optJSONArray("arguments")?.opt(index) ?: JSONObject.NULL
26
-
27
- /** The object the call is made on. */
28
- fun <T : Any> target(type: Class<T>): T = obj(request.opt("target"), type)
29
-
30
- /** The handlers an implementation calls. */
31
- val methods: Set<String> by lazy {
32
- val methods = request.optJSONArray("methods") ?: return@lazy emptySet()
33
- (0 until methods.length()).map(methods::getString).toSet()
34
- }
35
-
36
- private val scope: String get() = request.optString("scope")
37
-
38
- fun boolean(value: Any?): Boolean = value as? Boolean ?: throw mismatch(value, "boolean")
39
-
40
- fun byte(value: Any?): Byte = long(value).let { if (it in Byte.MIN_VALUE..Byte.MAX_VALUE) it.toByte() else throw mismatch(value, "byte") }
41
-
42
- fun short(value: Any?): Short = long(value).let { if (it in Short.MIN_VALUE..Short.MAX_VALUE) it.toShort() else throw mismatch(value, "short") }
43
-
44
- fun int(value: Any?): Int = long(value).let { if (it in Int.MIN_VALUE..Int.MAX_VALUE) it.toInt() else throw mismatch(value, "int") }
45
-
46
- fun long(value: Any?): Long {
47
- val number = double(value)
48
- val integer = number.toLong()
49
- if (integer.toDouble() != number) throw mismatch(value, "long")
50
- return integer
51
- }
52
-
53
- fun float(value: Any?): Float = double(value).toFloat()
54
-
55
- fun double(value: Any?): Double =
56
- (value as? Number)?.toDouble() ?: string(tagged(value, "number")).toDoubleOrNull() ?: throw mismatch(value, "double")
57
-
58
- fun char(value: Any?): Char = string(value).singleOrNull() ?: throw mismatch(value, "char")
59
-
60
- fun string(value: Any?): String = value as? String ?: throw mismatch(value, "String")
61
-
62
- fun bytes(value: Any?): ByteArray = Base64.getDecoder().decode(string(tagged(value, "bytes")))
63
-
64
- /** The native object [value] refers to. */
65
- fun <T : Any> obj(value: Any?, type: Class<T>): T = resources.get(tagged(value, "object", "id"), type)
66
-
67
- /** A value of an erased or `Object` type: JSON as it is, or the native value a tag carries. */
68
- fun any(value: Any?): Any? =
69
- when (value) {
70
- null, JSONObject.NULL -> null
71
- is JSONArray -> list(value, ::any)
72
- is JSONObject ->
73
- when (value.optString("#")) {
74
- "object" -> obj(value, Any::class.java)
75
- "bytes" -> bytes(value)
76
- "number" -> double(value)
77
- else -> map(value, ::any)
78
- }
79
- else -> value
80
- }
81
-
82
- fun <T> optional(value: Any?, decode: (Any?) -> T): T? = if (value == null || value == JSONObject.NULL) null else decode(value)
83
-
84
- fun <T> list(value: Any?, decode: (Any?) -> T): List<T> {
85
- val values = value as? JSONArray ?: throw mismatch(value, "List")
86
- return (0 until values.length()).map { decode(values.get(it)) }
87
- }
88
-
89
- fun <T> map(value: Any?, decode: (Any?) -> T): Map<String, T> {
90
- val fields = (value as? JSONObject)?.let { if (it.optString("#") == "record") it.getJSONObject("value") else it }
91
- ?: throw mismatch(value, "Map")
92
- return fields.keys().asSequence().associateWith { decode(fields.get(it)) }
93
- }
94
-
95
- /**
96
- * [value] as JSON: values by their runtime type, and native objects as references kept in the
97
- * invocation's scope, with the names of their class and superclasses.
98
- */
99
- fun encode(value: Any?): Any =
100
- when (value) {
101
- null -> JSONObject.NULL
102
- is Boolean, is String, is Int, is Long, is Short, is Byte -> value
103
- is Double -> if (value.isFinite()) value else JSONObject().put("#", "number").put("value", value.toString())
104
- is Float -> encode(value.toDouble())
105
- is Char, is CharSequence -> value.toString()
106
- is ByteArray -> JSONObject().put("#", "bytes").put("value", Base64.getEncoder().encodeToString(value))
107
- is IntArray -> JSONArray(value.map(::encode))
108
- is LongArray -> JSONArray(value.map(::encode))
109
- is FloatArray -> JSONArray(value.map(::encode))
110
- is DoubleArray -> JSONArray(value.map(::encode))
111
- is BooleanArray -> JSONArray(value.map(::encode))
112
- is Array<*> -> JSONArray(value.map(::encode))
113
- is Iterable<*> -> JSONArray(value.map(::encode))
114
- is Map<*, *> -> record(value)
115
- is Enum<*> -> value.name
116
- is Throwable -> error(value)
117
- else -> JSONObject().put("#", "object").put("id", resources.retain(value, scope)).put("types", JSONArray(types(value)))
118
- }
119
-
120
- /** Replies with the JSON [body] returns, or the exception it throws. */
121
- fun run(body: () -> Any?): (() -> Unit)? {
122
- reply(TokamakPluginResult.encoded(runCatching { encodePluginValue(body()) }, true))
123
- return null
124
- }
125
-
126
- /** Keeps the implementation [create] makes while the subscription lasts, and replies with its ID. */
127
- fun implement(create: (TokamakBinding) -> Any): () -> Unit {
128
- val scope = resources.open()
129
- send(resources.retain(create(this), scope))
130
- return { resources.end(scope) }
131
- }
132
-
133
- /**
134
- * Calls the handler [method], when the implementation has one, with the arguments it encodes,
135
- * and returns whether it had one.
136
- */
137
- fun call(method: String, arguments: (TokamakBinding) -> List<Any?>): Boolean {
138
- if (method !in methods) return false
139
- send(JSONObject().put("method", method).put("arguments", JSONArray(arguments(this))))
140
- return true
141
- }
142
-
143
- private fun send(value: Any?) {
144
- reply(TokamakPluginResult.encoded(runCatching { encodePluginValue(value) }, false))
145
- }
146
-
147
- private fun record(fields: Map<*, *>): JSONObject {
148
- val encoded = JSONObject()
149
- fields.forEach { (key, value) -> encoded.put(key.toString(), encode(value)) }
150
- return if (encoded.has("#")) JSONObject().put("#", "record").put("value", encoded) else encoded
151
- }
152
-
153
- private fun error(error: Throwable): JSONObject = failure(error).put("#", "error")
154
-
155
- /** The field [key] of [value] tagged [tag]. */
156
- private fun tagged(value: Any?, tag: String, key: String = "value"): Any? {
157
- val fields = value as? JSONObject
158
- if (fields?.optString("#") != tag || !fields.has(key)) throw TokamakPluginError.typeError("Expected a tagged $tag")
159
- return fields.get(key)
160
- }
161
-
162
- private fun mismatch(value: Any?, type: String) = TokamakPluginError.typeError("${value?.javaClass?.simpleName} is not $type")
163
-
164
- /** The names of [value]'s class and superclasses, then of the interfaces they implement. */
165
- private fun types(value: Any): List<String> {
166
- val classes = generateSequence<Class<*>>(value.javaClass) { it.superclass }.toList()
167
- return (classes + classes.flatMap(::interfaces)).distinct().map { it.name }
168
- }
169
-
170
- /** The interfaces [type] implements, each followed by those it extends. */
171
- private fun interfaces(type: Class<*>): List<Class<*>> = type.interfaces.flatMap { listOf(it) + interfaces(it) }
172
- }