@@ -8,6 +8,7 @@ import com.github.jing332.script.toNativeArrayBuffer
8
8
import okhttp3.Response
9
9
import org.mozilla.javascript.Context
10
10
import org.mozilla.javascript.LambdaConstructor
11
+ import org.mozilla.javascript.ScriptRuntime
11
12
import org.mozilla.javascript.Scriptable
12
13
import org.mozilla.javascript.ScriptableObject
13
14
import org.mozilla.javascript.Undefined
@@ -55,13 +56,32 @@ class NativeResponse private constructor(val rawResponse: Response? = null) :
55
56
)
56
57
57
58
constructor .definePrototypeMethod<NativeResponse >(
58
- scope, " json" , 0 , { cx, scope, thisObj, args -> thisObj.js_json() }
59
+ scope, " json" , 0 , { cx, scope, thisObj, args ->
60
+ thisObj.js_json(ScriptRuntime .toBoolean(args.getOrNull(0 )))
61
+ }
59
62
)
60
63
constructor .definePrototypeMethod<NativeResponse >(
61
- scope, " text" , 0 , { cx, scope, thisObj, args -> thisObj.js_text() }
64
+ scope,
65
+ " text" ,
66
+ 0 ,
67
+ { cx, scope, thisObj, args ->
68
+ thisObj.js_text(
69
+ ScriptRuntime .toBoolean(
70
+ args.getOrNull(0 )
71
+ )
72
+ )
73
+ }
62
74
)
63
75
constructor .definePrototypeMethod<NativeResponse >(
64
- scope, " bytes" , 0 , { cx, scope, thisObj, args -> thisObj.js_bytes(cx, scope) }
76
+ scope,
77
+ " bytes" ,
78
+ 0 ,
79
+ { cx, scope, thisObj, args ->
80
+ thisObj.js_bytes(
81
+ cx, scope,
82
+ ScriptRuntime .toBoolean(args.getOrNull(0 ))
83
+ )
84
+ }
65
85
)
66
86
67
87
defineProperty(scope, CLASS_NAME , constructor , DONTENUM )
@@ -79,30 +99,32 @@ class NativeResponse private constructor(val rawResponse: Response? = null) :
79
99
return obj
80
100
}
81
101
82
- private fun NativeResponse.checkResponse (): Response {
83
- rawResponse ? : throw IllegalStateException (" rawResponse is null" )
84
- if (rawResponse.isSuccessful == true )
85
- return rawResponse
102
+ private fun NativeResponse.checkResponse (force : Boolean ): Response {
103
+ val resp = rawResponse ? : throw IllegalStateException (" rawResponse is null" )
104
+ if (force) return resp
105
+ if (resp.isSuccessful == true )
106
+ return resp
86
107
else
87
- throw Exception (" Response failed: code=${rawResponse? .code} , message=${rawResponse? .message} " )
108
+ throw Exception (" Response failed: code=${resp .code} , message=${resp .message} " )
88
109
}
89
110
90
- private fun NativeResponse.js_json (): Any = runScriptCatching {
91
- val resp = checkResponse()
111
+ private fun NativeResponse.js_json (force : Boolean ): Any = runScriptCatching {
112
+ val resp = checkResponse(force )
92
113
val str = resp.body?.string() ? : return @runScriptCatching " "
93
- JsonParser (Context .getCurrentContext(), this ).parseValue(str)
114
+ JsonParser (Context .getCurrentContext(), this ).parseValue(str)
94
115
}
95
116
96
- private fun NativeResponse.js_text (): Any = runScriptCatching {
97
- val resp = checkResponse()
98
- resp.body?.string() ? : " "
117
+ private fun NativeResponse.js_text (force : Boolean ): Any = runScriptCatching {
118
+ val resp = checkResponse(force )
119
+ resp.body?.string() ? : " "
99
120
}
100
121
101
- private fun NativeResponse.js_bytes (cx : Context , scope : Scriptable ): Any = runScriptCatching {
102
- val bytes = checkResponse().body?.bytes() ? : ByteArray (0 )
103
- val buffer = bytes.toNativeArrayBuffer()
122
+ private fun NativeResponse.js_bytes (cx : Context , scope : Scriptable , force : Boolean ): Any =
123
+ runScriptCatching {
124
+ val bytes = checkResponse(force).body?.bytes() ? : ByteArray (0 )
125
+ val buffer = bytes.toNativeArrayBuffer()
104
126
105
- cx.newObject(scope, " Uint8Array" , arrayOf(buffer, 0 , buffer.length))
106
- }
127
+ cx.newObject(scope, " Uint8Array" , arrayOf(buffer, 0 , buffer.length))
128
+ }
107
129
}
108
130
}
0 commit comments