All files / src/core struct.js

100% Statements 117/117
100% Branches 32/32
100% Functions 29/29
100% Lines 116/116
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297          23x 23x 23x 23x 23x     23x 23x 23x               23x 12x 12x 12x 12x   37x 37x 37x   37x         37x 37x 37x       12x                 90x 87x 87x           3x             87x 1x     86x 86x 86x 86x   86x 3x     83x 83x 83x 83x 83x     83x 4x 4x     83x 15x 15x     83x         83x     31x           90x   90x     90x 50x 50x 50x 50x         10x 10x 10x 10x 10x       9x 9x 9x 9x       9x         3x 3x     18x 18x 18x       1x   1x 3x         3x           3x               9x 24x   24x   9x             90x 90x 12x   12x                   12x   12x 12x       12x 12x           36x 5x         12x         12x     37x 37x     12x   90x 173x 173x 173x 171x     2x         2x             90x 840x 840x 840x   840x 643x       197x                       90x 119x 119x   119x 10x     109x 9x     100x            
/**
 * Structs Plugin
 *
 * @flow
 */
import Syntax from 'walt-syntax';
import invariant from 'invariant';
import { find } from 'walt-parser-tools/scope';
import walkNode from 'walt-parser-tools/walk-node';
import { extendNode } from '../utils/extend-node';
import type { NodeType, SemanticPlugin } from '../flow/types';
 
const STRUCT_NATIVE_TYPE = 'i32';
const DIRECT_ADDRESS = '__DIRECT_ADDRESS__';
const sizeMap = {
  i64: 8,
  f64: 8,
  i32: 4,
  f32: 4,
  [DIRECT_ADDRESS]: 4,
};
 
export const getByteOffsetsAndSize = (objectLiteralNode: NodeType) => {
  const offsetsByKey = {};
  const keyTypeMap = {};
  let size = 0;
  walkNode({
    [Syntax.Pair]: keyTypePair => {
      const [lhs] = keyTypePair.params;
      const key = lhs.value;
      const type = keyTypePair.params[1].value;
 
      invariant(
        offsetsByKey[key] == null,
        `Duplicate key ${key} not allowed in object type`
      );
 
      keyTypeMap[key] = `${lhs.Type === 'AddressOf' ? '&' : ''}${type}`;
      offsetsByKey[key] = size;
      size += sizeMap[type] || 4;
    },
  })(objectLiteralNode);
 
  return [offsetsByKey, size, keyTypeMap];
};
 
type StructType = {
  load: NodeType,
  store: any => NodeType,
  offset: NodeType,
  type: string,
};
const makeStruct = stmt => (base, field): StructType => {
  const unreachable = stmt`throw;`;
  const fatal = {
    load: extendNode(
      { range: field.range },
      stmt`i32.load(${unreachable}, ${unreachable});`
    ),
    store: rhs =>
      extendNode(
        { range: field.range },
        stmt`i32.store(${unreachable}, ${rhs});`
      ),
    offset: unreachable,
    type: 'void',
  };
  if (base.meta.STRUCT_TYPE == null) {
    return fatal;
  }
 
  const typedef = base.meta.STRUCT_TYPE;
  const offsetMap = typedef.meta.TYPE_OBJECT;
  const typeMap = typedef.meta.OBJECT_KEY_TYPES;
  const address = offsetMap[field.value];
 
  if (address == null) {
    return fatal;
  }
 
  let type = typeMap[field.value];
  const direct = type[0] === '&';
  const offset = address ? stmt`(${base} + ${address});` : stmt`(${base});`;
  let STRUCT_TYPE = null;
  let TYPE_ARRAY = null;
 
  // Nested stuct type access
  if (type != null && typeof type === 'object') {
    STRUCT_TYPE = type;
    type = STRUCT_NATIVE_TYPE;
  }
 
  if (String(type).endsWith('[]')) {
    TYPE_ARRAY = type.slice(0, -2).replace('&', '');
    type = 'i32';
  }
 
  const withMeta = extendNode({
    range: base.range,
    meta: { STRUCT_TYPE, TYPE_ARRAY },
  });
 
  return {
    offset,
    type,
    store: rhs => withMeta(stmt`${type}.store(${offset}, ${rhs});`),
    load: withMeta(direct ? offset : stmt`${type}.load(${offset});`),
  };
};
 
export default function Struct(): SemanticPlugin {
  return {
    semantics({ stmt }) {
      const structure = makeStruct(stmt);
 
      function access(_next) {
        return (args, transform) => {
          const [node, context] = args;
          const [lookup, key] = node.params;
          const s = structure(transform([lookup, context]), key);
          return transform([s.load, context]);
        };
      }
 
      function fieldAssignment(args, transform) {
        const [node, context] = args;
        const [lhs, rhs] = node.params;
        const [root, key] = lhs.params;
        const s = structure(transform([root, context]), key);
        return transform([s.store(rhs), context]);
      }
 
      function objectAssignment(args, transform) {
        const [node, context] = args;
        const [lhs, rhs] = node.params;
        const base = transform([lhs, context]);
        const kvs = [];
 
        // We have to walk the nodes twice, once for regular prop keys and then again
        // for ...(spread)
        walkNode({
          // Top level Identifiers _inside_ an object literal === shorthand
          // Notice that we ignore chld mappers in both Pairs and Spread(s) so the
          // only way this is hit is if the identifier is TOP LEVEL
          [Syntax.Identifier]: (value, _) => {
            const field = structure(base, value);
            kvs.push({ field, value });
          },
          [Syntax.Pair]: (pair, _) => {
            const [property, value] = pair.params;
            const field = structure(base, property);
            kvs.push({ field, value });
          },
          [Syntax.Spread]: (spread, _) => {
            // find userType
            const target = transform([spread.params[0], context]);
            // map over the keys
            Object.keys(target.meta.TYPE_OBJECT).forEach(key => {
              const field = structure(base, {
                value: key,
                type: null,
                range: target.range,
              });
              const s = structure(target, {
                value: key,
                type: null,
                range: target.range,
              });
 
              kvs.push({
                field,
                value: s.load,
              });
            });
          },
        })(rhs);
 
        const params: NodeType[] = kvs
          .filter(({ field }) => field != null)
          /* $FlowFixMe */
          .map(kv => transform([kv.field.store(kv.value), context]));
 
        return {
          ...lhs,
          Type: Syntax.Block,
          params: params,
        };
      }
 
      return {
        [Syntax.Struct]: _ => ([node, { userTypes }]) => {
          const [union] = node.params;
 
          const structNode = {
            ...node,
            meta: {
              ...node.meta,
              TYPE_OBJECT: {},
              OBJECT_SIZE: 0,
              OBJECT_KEY_TYPES: {},
            },
          };
 
          walkNode({
            [Syntax.ObjectLiteral]: obj => {
              const [offsets, size, typeMap] = getByteOffsetsAndSize(obj);
              structNode.meta.TYPE_OBJECT = {
                ...structNode.meta.TYPE_OBJECT,
                ...offsets,
              };
              structNode.meta.OBJECT_SIZE += size;
              structNode.meta.OBJECT_KEY_TYPES = {
                ...structNode.meta.OBJECT_KEY_TYPES,
                ...typeMap,
              };
            },
            [Syntax.Type]: type => {
              if (String(type.type).endsWith('[]')) {
                structNode.meta.TYPE_ARRAY = type.type.slice(0, -2);
              }
            },
          })(union);
 
          userTypes[structNode.value] = structNode;
 
          // Map over the strings for key types and replace them with struct
          // references where necessary. We do this after creating the object
          // to allow for self-referencing structs (linked lists etc)
          structNode.meta.OBJECT_KEY_TYPES = Object.entries(
            structNode.meta.OBJECT_KEY_TYPES
          ).reduce((acc, [key, value]) => {
            acc[key] = userTypes[value] || value;
            return acc;
          }, {});
 
          return structNode;
        },
        [Syntax.FunctionResult]: next => (args, transform) => {
          const [node, context] = args;
          const { userTypes } = context;
          if (!userTypes[String(node.type)]) {
            return next(args);
          }
 
          return next([
            extendNode(
              {
                type: STRUCT_NATIVE_TYPE,
                meta: { STRUCT_TYPE: userTypes[node.type] },
                params: node.params.map(p => transform([p, context])),
              },
              node
            ),
            context,
          ]);
        },
        [Syntax.Identifier]: next => args => {
          const [node, context] = args;
          const { userTypes, scopes } = context;
          const ref = find(scopes, node.value);
          // Ignore anything not typed as a struct
          if (!(ref && userTypes[ref.type])) {
            return next(args);
          }
 
          // Convert all struct uses to STRUCT_NATIVE_TYPE types
          return {
            ...node,
            meta: {
              ...node.meta,
              ...ref.meta,
              ...userTypes[ref.type].meta,
              STRUCT_TYPE: userTypes[ref.type],
            },
            type: STRUCT_NATIVE_TYPE,
          };
        },
        [Syntax.Access]: access,
        [Syntax.Assignment]: next => (args, transform) => {
          const [node] = args;
          const [lhs, rhs] = node.params;
 
          if (lhs.Type === Syntax.Access) {
            return fieldAssignment(args, transform);
          }
 
          if (rhs.Type === Syntax.ObjectLiteral) {
            return objectAssignment(args, transform);
          }
 
          return next(args);
        },
      };
    },
  };
}