nashorn/test/script/basic/javaarrayconversion.js
author martin
Wed, 10 Dec 2014 09:23:00 -0800
changeset 28057 1a47ceecdba5
parent 24778 2ff5d7041566
child 32693 7da64fc12993
permissions -rw-r--r--
8065804: JEP 171: Clarifications/corrections for fence intrinsics Reviewed-by: dholmes, shade, dl, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 17758
diff changeset
     4
 *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 17758
diff changeset
     8
 *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * accompanied this code).
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 17758
diff changeset
    14
 *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 17758
diff changeset
    18
 *
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
 * Tests for conversion of JavaScript arrays to Java arrays and the other
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 17758
diff changeset
    26
 * way round. Also generally useful as a JavaScript-to-Java type conversion
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
 * test.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
 * @test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
 * @run
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
var x; // used for undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
var testCount = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
function testF(inputValue, type, testFn) {
17758
2b056941e4dd 8014797: rename Java.toJavaArray/toJavaScriptArray to Java.to/from, respectively.
attila
parents: 16151
diff changeset
    37
  var x = Java.to([inputValue], type + "[]")[0];
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
  if(!testFn(x)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
    throw ("unexpected value: " + x)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
  }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
  ++testCount;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
function test(inputValue, type, expectedValue) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
  testF(inputValue, type, function(x) { return x === expectedValue })
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
function testNaN(inputValue, type) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
  testF(inputValue, type, isNaN)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 17758
diff changeset
    52
// Those labeled "Correct?" are not clearly correct conversions. Those
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 17758
diff changeset
    53
// labeled "TypeError maybe?" could actually throw a TypeError, or only
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 17758
diff changeset
    54
// throw a TypeError when in strict mode.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
// The case of ("false", "boolean") => true is particularly amusing.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
test(x, "int", 0) // Correct? TypeError maybe?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
test(null, "int", 0) // Correct? TypeError maybe?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
test(1234, "int", 1234)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
test("1234", "int", 1234)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
test("1234.49", "int", 1234)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
test("1234.51", "int", 1234) // truncates, not rounds
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
test(true, "int", 1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
test(false, "int", 0)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
test("foo", "int", 0) // Correct? TypeError maybe?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
test(x, "boolean", false) // Correct? TypeError maybe?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
test(null, "boolean", false) // Correct? TypeError maybe?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
test(0, "boolean", false)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
test(1234, "boolean", true)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
test("foo", "boolean", true)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
test("", "boolean", false)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
test("false", "boolean", true) // Correct? false maybe?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
test(x, "java.lang.String", "undefined") // Correct? TypeError maybe?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
test(null, "java.lang.String", null)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
test(1234, "java.lang.String", "1234")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
test(1234.5, "java.lang.String", "1234.5")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
test(true, "java.lang.String", "true")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
test(false, "java.lang.String", "false")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
test(x, "java.lang.Integer", null) // Correct? TypeError maybe?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
test(null, "java.lang.Integer", null)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
test(1234, "java.lang.Integer", 1234)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
test("1234", "java.lang.Integer", 1234)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
test("1234.49", "java.lang.Integer", 1234)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
test("1234.51", "java.lang.Integer", 1234) // truncates, not rounds
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
test(true, "java.lang.Integer", 1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
test(false, "java.lang.Integer", 0)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
test("foo", "java.lang.Integer", 0) // Correct? TypeError maybe?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
test(x, "java.lang.Boolean", null) // Correct? TypeError maybe?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
test(null, "java.lang.Boolean", null)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
test(0, "java.lang.Boolean", false)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
test(1234, "java.lang.Boolean", true)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
test("foo", "java.lang.Boolean", true)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
test("", "java.lang.Boolean", false)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
test("false", "java.lang.Boolean", true) // Correct? false maybe?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
testNaN(x, "double")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
test(null, "double", 0)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
test(1234, "double", 1234)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
test("1234", "double", 1234)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
test("1234.5", "double", 1234.5)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
test(true, "double", 1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
test(false, "double", 0)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
testNaN("foo", "double")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
testNaN(x, "java.lang.Double")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
test(null, "java.lang.Double", null)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
test(1234, "java.lang.Double", 1234)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
test("1234", "java.lang.Double", 1234)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
test("1234.5", "java.lang.Double", 1234.5)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
test(true, "java.lang.Double", 1)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
test(false, "java.lang.Double", 0)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
testNaN("foo", "java.lang.Double")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
test({ valueOf: function() { return 42; } }, "int", 42)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
test({ valueOf: function() { return "42"; } }, "int", 42)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
// If there's no valueOf, toString is used
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
test({ toString: function() { return "42"; } }, "int", 42)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
// For numbers, valueOf takes precedence over toString
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
test({ valueOf: function() { return "42"; },  toString: function() { return "43"; } }, "int", 42)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
test({ toString: function() { return "foo"; } }, "java.lang.String", "foo")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
// Yep, even if we have valueOf, toString from prototype takes precedence
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
test({ valueOf: function() { return 42; } }, "java.lang.String", "[object Object]")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
// Converting to string, toString takes precedence over valueOf
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
test({ valueOf: function() { return "42"; },  toString: function() { return "43"; } }, "java.lang.String", "43")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
function assertCantConvert(sourceType, targetType) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
  try {
17758
2b056941e4dd 8014797: rename Java.toJavaArray/toJavaScriptArray to Java.to/from, respectively.
attila
parents: 16151
diff changeset
   133
    Java.to([new Java.type(sourceType)()], targetType + "[]")
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
    throw "no TypeError encountered"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
  } catch(e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
      if(!(e instanceof TypeError)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
        throw e;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
      }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
      ++testCount;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
  }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
// Arbitrary POJOs can't be converted to Java values
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
assertCantConvert("java.util.BitSet", "int")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
assertCantConvert("java.util.BitSet", "double")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
assertCantConvert("java.util.BitSet", "long")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
assertCantConvert("java.util.BitSet", "boolean")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
assertCantConvert("java.util.BitSet", "java.lang.String")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
assertCantConvert("java.util.BitSet", "java.lang.Double")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
assertCantConvert("java.util.BitSet", "java.lang.Long")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
/***************************************************************************
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
 * Now testing the other way round - Java arrays & collections to JavaScript
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
 **************************************************************************/
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
function assert(x) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
  if(!x) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
    throw "Assertion failed"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
  }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
  ++testCount;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
var intArray = new (Java.type("int[]"))(3)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
intArray[0] = 1234;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
intArray[1] = 42;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
intArray[2] = 5;
17758
2b056941e4dd 8014797: rename Java.toJavaArray/toJavaScriptArray to Java.to/from, respectively.
attila
parents: 16151
diff changeset
   167
var jsIntArray = Java.from(intArray)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
assert(jsIntArray instanceof Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
assert(jsIntArray[0] === 1234);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
assert(jsIntArray[1] === 42);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
assert(jsIntArray[2] === 5);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
// The arrays are copies, they don't reflect each other
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
intArray[2] = 6;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
assert(jsIntArray[2] === 5);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
jsIntArray[2] = 7;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
assert(intArray[2] === 6);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
var byteArray = new (Java.type("byte[]"))(2)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
byteArray[0] = -128;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
byteArray[1] = 127;
17758
2b056941e4dd 8014797: rename Java.toJavaArray/toJavaScriptArray to Java.to/from, respectively.
attila
parents: 16151
diff changeset
   182
var jsByteArray = Java.from(byteArray)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
assert(jsByteArray instanceof Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
assert(jsByteArray[0] === -128);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
assert(jsByteArray[1] === 127);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
var shortArray = new (Java.type("short[]"))(2)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
shortArray[0] = -32768;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
shortArray[1] = 32767;
17758
2b056941e4dd 8014797: rename Java.toJavaArray/toJavaScriptArray to Java.to/from, respectively.
attila
parents: 16151
diff changeset
   190
var jsShortArray = Java.from(shortArray)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
assert(jsShortArray instanceof Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
assert(jsShortArray[0] === -32768);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
assert(jsShortArray[1] === 32767);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
var floatArray = new (Java.type("float[]"))(2)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
floatArray[0] = java.lang.Float.MIN_VALUE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
floatArray[1] = java.lang.Float.MAX_VALUE;
17758
2b056941e4dd 8014797: rename Java.toJavaArray/toJavaScriptArray to Java.to/from, respectively.
attila
parents: 16151
diff changeset
   198
var jsFloatArray = Java.from(floatArray)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
assert(jsFloatArray instanceof Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
assert(jsFloatArray[0] == java.lang.Float.MIN_VALUE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
assert(jsFloatArray[1] == java.lang.Float.MAX_VALUE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
var charArray = new (Java.type("char[]"))(3)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
charArray[0] = "a";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
charArray[1] = "b";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
charArray[2] = "1";
17758
2b056941e4dd 8014797: rename Java.toJavaArray/toJavaScriptArray to Java.to/from, respectively.
attila
parents: 16151
diff changeset
   207
var jsCharArray = Java.from(charArray)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
assert(jsCharArray instanceof Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
assert(jsCharArray[0] === 97);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
assert(jsCharArray[1] === 98);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
assert(jsCharArray[2] === 49);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
var booleanArray = new (Java.type("boolean[]"))(2)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
booleanArray[0] = true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
booleanArray[1] = false;
17758
2b056941e4dd 8014797: rename Java.toJavaArray/toJavaScriptArray to Java.to/from, respectively.
attila
parents: 16151
diff changeset
   216
var jsBooleanArray = Java.from(booleanArray)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
assert(jsBooleanArray instanceof Array);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
assert(jsBooleanArray[0] === true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
assert(jsBooleanArray[1] === false);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
print(testCount + " tests completed ok")