Boo

Unsafe code support (pointer manipulation within `unsafe' block)

Details

  • Type: New Feature New Feature
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 0.9.1
  • Fix Version/s: 0.9.2
  • Component/s: Compiler
  • Labels:
    None
  • Testcase included:
    yes
  • Number of attachments :
    0

Description

Usage:

unsafe [<ptrName> as <ptrType> = <data>]+:
    <body>

unsafe blocks are only allowed when "-unsafe" flag as been passed to the compiler (CompilerParameters.Unsafe == true), otherwise an error is reported.
It declares pointer(s) to one or more value type references (<data>).
In the context of an unsafe block, an explode operator on a declared pointer is replaced with the indirection operator.

sizeof built-in meta-method is introduced, it accepts both type references (as in C#) and also variable references.

Example:

src = array[of byte](8)
src[3] = 1
dst = array[of byte](8)
dst[6] = 1

#replace an uint at a time from src to dst only if src is not zero
unsafe psrc as uint = src, pdst as uint = dst:
	for i in range(len(src) / sizeof(psrc)):
		*pdst = *psrc unless *psrc == 0
		psrc++
		pdst++

assert dst[3] == 1 #this has been copied from src
assert dst[6] == 1 #this has not been replaced since src[4:8] is 0

Activity

Hide
Cedric Vivier added a comment -

Landed in rev. 3309

Show
Cedric Vivier added a comment - Landed in rev. 3309

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: