Skip to content

Commit

Permalink
lib/json: fix go1.17 build by avoiding reflect.Value.UnsafePointer (#474
Browse files Browse the repository at this point in the history
)

Fixes #473
  • Loading branch information
adonovan committed Jun 12, 2023
1 parent 99efbb1 commit 9532f56
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions lib/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,30 @@ import (

// Module json is a Starlark module of JSON-related functions.
//
// json = module(
// encode,
// decode,
// indent,
// )
// json = module(
// encode,
// decode,
// indent,
// )
//
// def encode(x):
//
// The encode function accepts one required positional argument,
// which it converts to JSON by cases:
// - A Starlark value that implements Go's standard json.Marshal
// interface defines its own JSON encoding.
// - None, True, and False are converted to null, true, and false, respectively.
// - Starlark int values, no matter how large, are encoded as decimal integers.
// Some decoders may not be able to decode very large integers.
// - Starlark float values are encoded using decimal point notation,
// even if the value is an integer.
// It is an error to encode a non-finite floating-point value.
// - Starlark strings are encoded as JSON strings, using UTF-16 escapes.
// - a Starlark IterableMapping (e.g. dict) is encoded as a JSON object.
// It is an error if any key is not a string.
// - any other Starlark Iterable (e.g. list, tuple) is encoded as a JSON array.
// - a Starlark HasAttrs (e.g. struct) is encoded as a JSON object.
// - A Starlark value that implements Go's standard json.Marshal
// interface defines its own JSON encoding.
// - None, True, and False are converted to null, true, and false, respectively.
// - Starlark int values, no matter how large, are encoded as decimal integers.
// Some decoders may not be able to decode very large integers.
// - Starlark float values are encoded using decimal point notation,
// even if the value is an integer.
// It is an error to encode a non-finite floating-point value.
// - Starlark strings are encoded as JSON strings, using UTF-16 escapes.
// - a Starlark IterableMapping (e.g. dict) is encoded as a JSON object.
// It is an error if any key is not a string.
// - any other Starlark Iterable (e.g. list, tuple) is encoded as a JSON array.
// - a Starlark HasAttrs (e.g. struct) is encoded as a JSON object.
//
// It an application-defined type matches more than one the cases describe above,
// (e.g. it implements both Iterable and HasFields), the first case takes precedence.
// Encoding any other value yields an error.
Expand All @@ -57,10 +58,11 @@ import (
//
// The decode function has one required positional parameter, a JSON string.
// It returns the Starlark value that the string denotes.
// - Numbers are parsed as int or float, depending on whether they
// contain a decimal point.
// - JSON objects are parsed as new unfrozen Starlark dicts.
// - JSON arrays are parsed as new unfrozen Starlark lists.
// - Numbers are parsed as int or float, depending on whether they
// contain a decimal point.
// - JSON objects are parsed as new unfrozen Starlark dicts.
// - JSON arrays are parsed as new unfrozen Starlark lists.
//
// If x is not a valid JSON string, the behavior depends on the "default"
// parameter: if present, Decode returns its value; otherwise, Decode fails.
//
Expand All @@ -71,7 +73,6 @@ import (
// It accepts one required positional parameter, the JSON string,
// and two optional keyword-only string parameters, prefix and indent,
// that specify a prefix of each new line, and the unit of indentation.
//
var Module = &starlarkstruct.Module{
Name: "json",
Members: starlark.StringDict{
Expand Down Expand Up @@ -235,7 +236,8 @@ func pointer(i interface{}) unsafe.Pointer {
v := reflect.ValueOf(i)
switch v.Kind() {
case reflect.Ptr, reflect.Chan, reflect.Map, reflect.UnsafePointer, reflect.Slice:
return v.UnsafePointer()
// TODO(adonovan): use v.Pointer() when we drop go1.17.
return unsafe.Pointer(v.Pointer())
default:
return nil
}
Expand Down

0 comments on commit 9532f56

Please sign in to comment.