Adding upstream version 0.0~git20250520.a1d9079+dfsg.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
590ac7ff5f
commit
20149b7f3a
456 changed files with 70406 additions and 0 deletions
153
bind/seq/ref.go
Normal file
153
bind/seq/ref.go
Normal file
|
@ -0,0 +1,153 @@
|
|||
// Copyright 2014 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package seq
|
||||
|
||||
//#cgo LDFLAGS: -llog
|
||||
//#include <android/log.h>
|
||||
//#include <string.h>
|
||||
//import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type countedObj struct {
|
||||
obj interface{}
|
||||
cnt int32
|
||||
}
|
||||
|
||||
// also known to bind/java/Seq.java and bind/objc/seq_darwin.m
|
||||
const NullRefNum = 41
|
||||
|
||||
// refs stores Go objects that have been passed to another language.
|
||||
var refs struct {
|
||||
sync.Mutex
|
||||
next int32 // next reference number to use for Go object, always negative
|
||||
refs map[interface{}]int32
|
||||
objs map[int32]countedObj
|
||||
}
|
||||
|
||||
func init() {
|
||||
refs.Lock()
|
||||
refs.next = -24 // Go objects get negative reference numbers. Arbitrary starting point.
|
||||
refs.refs = make(map[interface{}]int32)
|
||||
refs.objs = make(map[int32]countedObj)
|
||||
refs.Unlock()
|
||||
}
|
||||
|
||||
// A Ref represents a Java or Go object passed across the language
|
||||
// boundary.
|
||||
type Ref struct {
|
||||
Bind_Num int32
|
||||
}
|
||||
|
||||
type proxy interface {
|
||||
// Use a strange name and hope that user code does not implement it
|
||||
Bind_proxy_refnum__() int32
|
||||
}
|
||||
|
||||
// ToRefNum increments the reference count for an object and
|
||||
// returns its refnum.
|
||||
func ToRefNum(obj interface{}) int32 {
|
||||
// We don't track foreign objects, so if obj is a proxy
|
||||
// return its refnum.
|
||||
if r, ok := obj.(proxy); ok {
|
||||
refnum := r.Bind_proxy_refnum__()
|
||||
if refnum <= 0 {
|
||||
panic(fmt.Errorf("seq: proxy contained invalid Go refnum: %d", refnum))
|
||||
}
|
||||
return refnum
|
||||
}
|
||||
refs.Lock()
|
||||
num := refs.refs[obj]
|
||||
if num != 0 {
|
||||
s := refs.objs[num]
|
||||
refs.objs[num] = countedObj{s.obj, s.cnt + 1}
|
||||
} else {
|
||||
num = refs.next
|
||||
refs.next--
|
||||
if refs.next > 0 {
|
||||
panic("refs.next underflow")
|
||||
}
|
||||
refs.refs[obj] = num
|
||||
refs.objs[num] = countedObj{obj, 1}
|
||||
}
|
||||
refs.Unlock()
|
||||
|
||||
return num
|
||||
}
|
||||
|
||||
// FromRefNum returns the Ref for a refnum. If the refnum specifies a
|
||||
// foreign object, a finalizer is set to track its lifetime.
|
||||
func FromRefNum(num int32) *Ref {
|
||||
if num == NullRefNum {
|
||||
return nil
|
||||
}
|
||||
ref := &Ref{num}
|
||||
if num > 0 {
|
||||
// This is a foreign object reference.
|
||||
// Track its lifetime with a finalizer.
|
||||
runtime.SetFinalizer(ref, FinalizeRef)
|
||||
}
|
||||
|
||||
return ref
|
||||
}
|
||||
|
||||
// Bind_IncNum increments the foreign reference count and
|
||||
// return the refnum.
|
||||
func (r *Ref) Bind_IncNum() int32 {
|
||||
refnum := r.Bind_Num
|
||||
IncForeignRef(refnum)
|
||||
// Make sure this reference is not finalized before
|
||||
// the foreign reference count is incremented.
|
||||
runtime.KeepAlive(r)
|
||||
return refnum
|
||||
}
|
||||
|
||||
// Get returns the underlying object.
|
||||
func (r *Ref) Get() interface{} {
|
||||
refnum := r.Bind_Num
|
||||
refs.Lock()
|
||||
o, ok := refs.objs[refnum]
|
||||
refs.Unlock()
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("unknown ref %d", refnum))
|
||||
}
|
||||
// This is a Go reference and its refnum was incremented
|
||||
// before crossing the language barrier.
|
||||
Delete(refnum)
|
||||
return o.obj
|
||||
}
|
||||
|
||||
// Inc increments the reference count for a refnum. Called from Bind_proxy_refnum
|
||||
// functions.
|
||||
func Inc(num int32) {
|
||||
refs.Lock()
|
||||
o, ok := refs.objs[num]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("seq.Inc: unknown refnum: %d", num))
|
||||
}
|
||||
refs.objs[num] = countedObj{o.obj, o.cnt + 1}
|
||||
refs.Unlock()
|
||||
}
|
||||
|
||||
// Delete decrements the reference count and removes the pinned object
|
||||
// from the object map when the reference count becomes zero.
|
||||
func Delete(num int32) {
|
||||
refs.Lock()
|
||||
defer refs.Unlock()
|
||||
o, ok := refs.objs[num]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("seq.Delete unknown refnum: %d", num))
|
||||
}
|
||||
if o.cnt <= 1 {
|
||||
delete(refs.objs, num)
|
||||
delete(refs.refs, o.obj)
|
||||
} else {
|
||||
refs.objs[num] = countedObj{o.obj, o.cnt - 1}
|
||||
}
|
||||
}
|
21
bind/seq/seq.go
Normal file
21
bind/seq/seq.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2014 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package seq implements the machine-dependent seq serialization format.
|
||||
//
|
||||
// Implementations of Transact and FinalizeRef are provided by a
|
||||
// specific foreign language binding package, e.g. go.mobile/bind/java.
|
||||
//
|
||||
// Designed only for use by the code generated by gobind. Don't try to
|
||||
// use this directly.
|
||||
package seq
|
||||
|
||||
import _ "golang.org/x/mobile/internal/mobileinit"
|
||||
|
||||
// FinalizeRef is the finalizer used on foreign objects.
|
||||
var FinalizeRef func(ref *Ref)
|
||||
|
||||
// IncRef increments the foreign reference count for ref while it is in transit.
|
||||
// The count is decremented after the ref is received and translated on the foreign side.
|
||||
var IncForeignRef func(refnum int32)
|
49
bind/seq/string.go
Normal file
49
bind/seq/string.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
// Copyright 2014 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package seq
|
||||
|
||||
import "unicode/utf16"
|
||||
|
||||
// Based heavily on package unicode/utf16 from the Go standard library.
|
||||
|
||||
const (
|
||||
replacementChar = '\uFFFD' // Unicode replacement character
|
||||
maxRune = '\U0010FFFF' // Maximum valid Unicode code point.
|
||||
)
|
||||
|
||||
const (
|
||||
// 0xd800-0xdc00 encodes the high 10 bits of a pair.
|
||||
// 0xdc00-0xe000 encodes the low 10 bits of a pair.
|
||||
// the value is those 20 bits plus 0x10000.
|
||||
surr1 = 0xd800
|
||||
surr2 = 0xdc00
|
||||
surr3 = 0xe000
|
||||
|
||||
surrSelf = 0x10000
|
||||
)
|
||||
|
||||
// UTF16Encode utf16 encodes s into chars. It returns the resulting
|
||||
// length in units of uint16. It is assumed that the chars slice
|
||||
// has enough room for the encoded string.
|
||||
func UTF16Encode(s string, chars []uint16) int {
|
||||
n := 0
|
||||
for _, v := range s {
|
||||
switch {
|
||||
case v < 0, surr1 <= v && v < surr3, v > maxRune:
|
||||
v = replacementChar
|
||||
fallthrough
|
||||
case v < surrSelf:
|
||||
chars[n] = uint16(v)
|
||||
n += 1
|
||||
default:
|
||||
// surrogate pair, two uint16 values
|
||||
r1, r2 := utf16.EncodeRune(v)
|
||||
chars[n] = uint16(r1)
|
||||
chars[n+1] = uint16(r2)
|
||||
n += 2
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
28
bind/seq/string_test.go
Normal file
28
bind/seq/string_test.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2014 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package seq
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"unicode/utf16"
|
||||
)
|
||||
|
||||
var strData = []string{
|
||||
"abcxyz09{}",
|
||||
"Hello, 世界",
|
||||
string([]rune{0xffff, 0x10000, 0x10001, 0x12345, 0x10ffff}),
|
||||
}
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
for _, test := range strData {
|
||||
chars := make([]uint16, 4*len(test))
|
||||
nchars := UTF16Encode(test, chars)
|
||||
chars = chars[:nchars]
|
||||
got := string(utf16.Decode(chars))
|
||||
if got != test {
|
||||
t.Errorf("UTF16: got %q, want %q", got, test)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue