Example #1
0
/**
 * VMNative: string_length( workshop )
 * Accepts one number argument: the string workshop instance
 */
static bool vmn_str_length(VM * vm, VMArg * arg, int argc) {
  VMLibData * data;

  /* check for proper number of arguments */
  if(argc != 1) {
    vm_set_err(vm, VMERR_INCORRECT_NUMARGS);
    return false;
  }

  /* check argument major type */
  if(vmarg_type(arg[0]) != TYPE_LIBDATA) {
    vm_set_err(vm, VMERR_INVALID_TYPE_ARGUMENT);
    return false;
  }

  /* extract the libdata from the argument */
  data = vmarg_libdata(arg[0]);

  /* check libdata type */
  if(!vmlibdata_is_type(data, LIBSTR_STRING_TYPE, LIBSTR_STRING_TYPE_LEN)) {
    vm_set_err(vm, VMERR_INVALID_TYPE_ARGUMENT);
    return false;
  }

  /* push string length */
  vmarg_push_number(vm, libstr_string_length(data));

  /* this function does return a value */
  return true;
}
Example #2
0
/**
 * VMNative: math_round( value, precision )
 * Accepts one number argument. Returns value rounded to precision decimal places.
 */
static bool vmn_math_round(VM * vm, VMArg * arg, int argc) {

  switch(argc) {
  case 1: /* If user did not specify precision, assume round to int */
   
    /* check argument type */
    if(vmarg_type(arg[0]) != TYPE_NUMBER) {
      vm_set_err(vm, VMERR_INVALID_TYPE_ARGUMENT);
      return false;
    }

    /* push result */
    vmarg_push_number(vm, round(vmarg_number(arg[0], NULL)));

    /* this function does return a value */
    return true;
  
  case 2: /* if user specified precision */

    /* check argument type */
    if(vmarg_type(arg[0]) != TYPE_NUMBER
       || vmarg_type(arg[1]) != TYPE_NUMBER) {
      vm_set_err(vm, VMERR_INVALID_TYPE_ARGUMENT);
      return false;
    }

    /* push result */
    vmarg_push_number(vm, round(vmarg_number(arg[0], NULL) 
				* pow(10, vmarg_number(arg[1], NULL)))
		      / pow(10, vmarg_number(arg[1], NULL)));

    /* this function does return a value */
    return true;
  }
  
  vm_set_err(vm, VMERR_INCORRECT_NUMARGS);
  return false;
}
Example #3
0
/**
 * VMNative: string_char_at( workshop, index )
 * Accepts one number argument: the string workshop instance
 * Returns the desired char as a TYPE_NUMBER.
 */
static bool vmn_str_char_at(VM * vm, VMArg * arg, int argc) {
  VMLibData * data;
  Buffer * buffer;
  int index;

  /* check for proper number of arguments */
  if(argc != 2) {
    vm_set_err(vm, VMERR_INCORRECT_NUMARGS);
    return false;
  }

  /* check argument major type */
  if(vmarg_type(arg[0]) != TYPE_LIBDATA ||
     vmarg_type(arg[1]) != TYPE_NUMBER) {
    vm_set_err(vm, VMERR_INVALID_TYPE_ARGUMENT);
    return false;
  }

  /* extract the libdata from the argument */
  data = vmarg_libdata(arg[0]);

  /* check libdata type */
  if(!vmlibdata_is_type(data, LIBSTR_STRING_TYPE, LIBSTR_STRING_TYPE_LEN)) {
    vm_set_err(vm, VMERR_INVALID_TYPE_ARGUMENT);
    return false;
  }

  /* extract the buffer */
  buffer = vmlibdata_data(data);

  /* extract the index */
  index = vmarg_number(arg[1], NULL);

  /* check buffer size range */
  if(index < 0 || index >= buffer_size(buffer)) {
    vm_set_err(vm, VMERR_ARGUMENT_OUT_OF_RANGE);
    return false;
  }

  /* push char as a number */
  if(!vmarg_push_number(vm, buffer_get_buffer(buffer)[index] )) {
    vm_set_err(vm, VMERR_ALLOC_FAILED);
    return false;
  }

  /* this function does return a value */
  return true;
}
Example #4
0
/**
 * VMNative: math_sqrt( value )
 * Accepts one number argument. Returns its square root.
 */
static bool vmn_math_sqrt(VM * vm, VMArg * arg, int argc) {

  /* check for proper number of arguments */
  if(argc != 1) {
    vm_set_err(vm, VMERR_INCORRECT_NUMARGS);
    return false;
  }

  /* check argument type */
  if(vmarg_type(arg[0]) != TYPE_NUMBER) {
    vm_set_err(vm, VMERR_INVALID_TYPE_ARGUMENT);
    return false;
  }

  /* push result */
  vmarg_push_number(vm, sqrt(vmarg_number(arg[0], NULL)));

  /* this function does return a value */
  return true;
}