Skip to content

Commit

Permalink
Fixed unicode string conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
yglukhov committed Nov 2, 2017
1 parent efe377b commit 942decc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion jnim.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.3.4"
version = "0.3.5"
author = "Anatoly Galiulin"
description = "Java bridge for Nim"
license = "MIT"
Expand Down
20 changes: 11 additions & 9 deletions src/private/jni_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ type
JVMObject* = ref object {.inheritable.}
obj: jobject

####################################################################################################
####################################################################################################
# Exception handling

type
Expand All @@ -142,7 +142,7 @@ template checkException() =
let ex = theEnv.ExceptionOccurred(theEnv).newJVMObjectConsumingLocalRef
theEnv.ExceptionClear(theEnv)
raise newJavaException(ex)

macro callVM*(s: untyped): untyped =
result = quote do:
let res = `s`
Expand Down Expand Up @@ -330,9 +330,11 @@ proc equalsRaw*(v1, v2: JVMObject): jboolean =

proc jstringToStringAux(s: jstring): string =
assert(not s.isNil)
let sz = theEnv.GetStringUTFLength(theEnv, s)
result = newString(sz)
theEnv.GetStringUTFRegion(theEnv, s, 0, sz, addr result[0])
let numBytes = theEnv.GetStringUTFLength(theEnv, s)
result = newString(numBytes)
if numBytes != 0:
let numChars = theEnv.GetStringLength(theEnv, s)
theEnv.GetStringUTFRegion(theEnv, s, 0, numChars, addr result[0])

proc toStringRaw(o: jobject): string =
# This is low level ``toString`` version.
Expand Down Expand Up @@ -434,7 +436,7 @@ template genArrayType(typ, arrTyp: typedesc, typName: untyped): untyped =
newJVMObject(a.arr.jobject)

# getters/setters

proc `get typName Array`*(c: JVMClass, name: cstring): `JVM typName Array` =
checkInit
let j = callVM theEnv.GetStaticObjectField(theEnv, c.get, c.getStaticFieldId(name, seq[`typ`].jniSig).get)
Expand Down Expand Up @@ -549,7 +551,7 @@ template genField(typ: typedesc, typName: untyped): untyped =
else:
theEnv.`SetStatic typName Field`(theEnv, c.get, id.get, v)
checkException

proc `set typName`*(c: JVMClass, name: string, v: `typ`) =
checkInit
when `typ` is JVMObject:
Expand Down Expand Up @@ -579,7 +581,7 @@ template genField(typ: typedesc, typName: untyped): untyped =
else:
theEnv.`Set typName Field`(theEnv, o.get, id.get, v)
checkException

proc `set typName`*(o: JVMObject, name: string, v: `typ`) =
checkInit
when `typ` is JVMObject:
Expand All @@ -601,7 +603,7 @@ template genField(typ: typedesc, typName: untyped): untyped =
checkInit
theEnv.`SetStatic typName Field`(theEnv, c.get, id.get, v)
checkException

proc setPropRaw*(T: typedesc[`typ`], o: JVMObject, id: JVMFieldID, v: jobject) =
checkInit
theEnv.`Set typName Field`(theEnv, o.get, id.get, v)
Expand Down

0 comments on commit 942decc

Please sign in to comment.