Example usage for jdk.nashorn.internal.objects NativeArray set

List of usage examples for jdk.nashorn.internal.objects NativeArray set

Introduction

In this page you can find the example usage for jdk.nashorn.internal.objects NativeArray set.

Prototype

@Override
    public void set(final Object key, final int value, final int callSiteFlags) 

Source Link

Usage

From source file:com.threecrickets.jvm.json.nashorn.util.NashornTokener.java

License:Open Source License

/**
 * Create a Rhino NativeArray.//from  ww  w .j av a2s  .co m
 * 
 * @return A NativeArray
 * @throws JsonSyntaxError
 *         In case of a JSON conversion error
 */
public ScriptObject createNativeArray() throws JsonSyntaxError {
    NativeArray nativeArray = NashornNativeUtil.newArray(0);
    int arrayIndex = 0;
    char c = nextClean();
    char q;

    if (c == '[') {
        q = ']';
    } else if (c == '(') {
        q = ')';
    } else {
        throw syntaxError("A JSON array text must start with '['");
    }
    if (nextClean() == ']') {
        return nativeArray;
    }
    back();
    for (;;) {
        if (nextClean() == ',') {
            back();
            nativeArray.set(arrayIndex++, null, 0);
        } else {
            back();
            nativeArray.set(arrayIndex++, nextValue(), 0);
        }
        c = nextClean();
        switch (c) {
        case ';':
        case ',':
            if (nextClean() == ']') {
                return nativeArray;
            }
            back();
            break;
        case ']':
        case ')':
            if (q != c) {
                throw syntaxError("Expected a '" + new Character(q) + "'");
            }
            return nativeArray;
        default:
            throw syntaxError("Expected a ',' or ']'");
        }
    }
}

From source file:org.bson.jvm.nashorn.NativeArrayCodec.java

License:Apache License

public NativeArray decode(BsonReader reader, DecoderContext decoderContext) {
    List<Object> list = new ArrayList<Object>();

    reader.readStartArray();//from   ww  w .  j ava  2s .c  o  m
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        Object item = BsonUtil.read(reader, decoderContext, codecRegistry, bsonTypeClassMap);
        list.add(item);
    }
    reader.readEndArray();

    NativeArray nativeArray = (NativeArray) NativeArray.construct(true, null, list.size());
    int index = 0;
    for (Object item : list)
        nativeArray.set(index++, item, 0);
    return nativeArray;
}