Mercurial > hg > truffle
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/access/SLWritePropertyNode.java @ 18411:dc2e000bed40
SL: add basic support for creating objects and accessing properties
author | Andreas Woess <andreas.woess@jku.at> |
---|---|
date | Tue, 18 Nov 2014 23:02:58 +0100 |
parents | |
children | 997bc9764a9a |
comparison
equal
deleted
inserted
replaced
18410:f444ef4684ec | 18411:dc2e000bed40 |
---|---|
1 /* | |
2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.sl.nodes.access; | |
24 | |
25 import java.util.*; | |
26 | |
27 import com.oracle.truffle.api.frame.*; | |
28 import com.oracle.truffle.api.nodes.*; | |
29 import com.oracle.truffle.api.source.*; | |
30 import com.oracle.truffle.sl.*; | |
31 import com.oracle.truffle.sl.nodes.*; | |
32 | |
33 /** | |
34 * The node for setting a property of an object. When executed, this node first evaluates the value | |
35 * expression on the right hand side of the equals operator, followed by the object expression on | |
36 * the left side of the dot operator, and then sets the named property of this object to the new | |
37 * value if the property already exists or adds a new property. Finally, it returns the new value. | |
38 */ | |
39 @NodeInfo(shortName = ".=") | |
40 public final class SLWritePropertyNode extends SLExpressionNode { | |
41 | |
42 public static SLWritePropertyNode create(SourceSection src, SLExpressionNode receiverNode, String propertyName, SLExpressionNode valueNode) { | |
43 return new SLWritePropertyNode(src, receiverNode, propertyName, valueNode); | |
44 } | |
45 | |
46 @Child protected SLExpressionNode receiverNode; | |
47 protected final String propertyName; | |
48 @Child protected SLExpressionNode valueNode; | |
49 | |
50 private SLWritePropertyNode(SourceSection src, SLExpressionNode receiverNode, String propertyName, SLExpressionNode valueNode) { | |
51 super(src); | |
52 this.receiverNode = receiverNode; | |
53 this.propertyName = propertyName; | |
54 this.valueNode = valueNode; | |
55 } | |
56 | |
57 @SuppressWarnings("unchecked") | |
58 @Override | |
59 public Object executeGeneric(VirtualFrame frame) { | |
60 Object value = valueNode.executeGeneric(frame); | |
61 Object object = receiverNode.executeGeneric(frame); | |
62 if (object instanceof Map) { | |
63 ((Map<Object, Object>) object).put(propertyName, value); | |
64 } else { | |
65 throw new SLException("unexpected receiver type"); | |
66 } | |
67 return value; | |
68 } | |
69 } |